﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApAPPRoboticTask
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp;


namespace DnProcessSimulateCommands;

public class CApAPPRoboticTask : CApAPPTask
{
  private TxRobot m_robot;
  private ArrayList m_initialZones;
  private List<KiAppRoboticWaypointEx> m_initialRobWpList = new List<KiAppRoboticWaypointEx>();
  private KiAppCellPositioningDescriptorEx m_defaultDescriptor;
  private ITxGun m_gunActiveWhenReachingLocation;
  private ITxGun m_previousValidActiveGun;
  private List<ArrayList> m_origLocationParameters = new List<ArrayList>();
  private KiAppCellPositioningDescriptorEx m_previousDescriptor;
  private ITxOperation m_lastReachedOperation;
  private bool m_shouldSkipNextOperationStartedForwardEvent;
  private ITxRoboticControllerServices m_robotControllerServices;
  private List<ITxLocationOperation> m_nonProjectedElements;
  private List<ITxLocationOperation> m_olpCommandsElements;
  private List<ITxLocationOperation> m_nonReachableElements;
  private bool m_areWaypointCreatedWithJump;
  private List<string> m_lastSimulationErrorMessages;
  private bool m_isJointProfileValidForAPP = true;
  private bool m_isRobotConfigValidForApp = true;

  public CApAPPRoboticTask(
    ITxRoboticOrderedCompoundOperation i_operation,
    ref KiAppCellPositioningDescriptorEx i_defDescriptor)
    : base((ITxOperation) i_operation)
  {
    this.m_nonProjectedElements = new List<ITxLocationOperation>();
    this.m_olpCommandsElements = new List<ITxLocationOperation>();
    this.m_nonReachableElements = new List<ITxLocationOperation>();
    this.m_areWaypointCreatedWithJump = false;
    this.m_lastSimulationErrorMessages = new List<string>();
    if (i_operation == null)
      throw new ArgumentNullException();
    this.m_defaultDescriptor = i_defDescriptor != null ? new KiAppCellPositioningDescriptorEx(i_defDescriptor) : new KiAppCellPositioningDescriptorEx();
    this.m_robot = i_operation.Robot as TxRobot;
    this.m_robotControllerServices = this.GetRoboticControllerServices((ITxRobot) this.m_robot);
    this.CheckIsJointProfileValid();
    this.CheckIsRobotConfigValidForAPP();
    this.fillTaskWithSimulation(i_operation);
    i_defDescriptor = new KiAppCellPositioningDescriptorEx(this.m_defaultDescriptor);
  }

  public void fillTaskWithSimulation(
    ITxRoboticOrderedCompoundOperation i_roboticOperation)
  {
    if (i_roboticOperation == null)
      return;
    if (TxFrame.op_Inequality(this.GetRobotTCPF(), (ITxObject) null))
      this.Robot.TCPF.AbsoluteLocation = this.GetRobotTCPF().AbsoluteLocation;
    using (new CApAPPViewerDisabler())
    {
      this.m_initialZones = this.GetInitialZones((ITxRoboticOperation) i_roboticOperation);
      this.m_origLocationParameters = this.GetInitialParameters((ITxRoboticOperation) i_roboticOperation);
      bool flag1 = this.createWaypointsWithSimulation(i_roboticOperation);
      if (flag1)
      {
        bool flag2 = TxApplicationEx.IsInEnabledMode(new List<TxApplicationModeEx>()
        {
          new TxApplicationModeEx("SIMULATION_WITHOUT_REFRESH_MODE", false)
        });
        if (flag2)
          TxApplicationEx.EnterMode(new TxApplicationModeEx("SIMULATION_WITHOUT_REFRESH_MODE", true));
        this.RestoreInitialState();
        bool flag3 = this.configureWaypointsWithJump(i_roboticOperation);
        this.RestoreInitialState();
        if (flag2)
          TxApplicationEx.ExitMode(new TxApplicationModeEx("SIMULATION_WITHOUT_REFRESH_MODE", true));
        flag1 = flag3;
      }
      this.FillDescriptorWithCurrentValues(this.m_defaultDescriptor);
      this.SetOperationZones((ITxRoboticOperation) i_roboticOperation, this.m_initialZones);
      this.m_areWaypointCreatedWithJump = false;
      if (!flag1)
      {
        bool flag4 = TxApplicationEx.IsInEnabledMode(new List<TxApplicationModeEx>()
        {
          new TxApplicationModeEx("SIMULATION_WITHOUT_REFRESH_MODE", false)
        });
        if (flag4)
          TxApplicationEx.EnterMode(new TxApplicationModeEx("SIMULATION_WITHOUT_REFRESH_MODE", true));
        this.RestoreInitialState();
        this.m_initialRobWpList = new List<KiAppRoboticWaypointEx>();
        this.m_areWaypointCreatedWithJump = true;
        this.createWaypointsWithJump(i_roboticOperation);
        this.RestoreInitialState();
        if (flag4)
          TxApplicationEx.ExitMode(new TxApplicationModeEx("SIMULATION_WITHOUT_REFRESH_MODE", true));
      }
      KiAppCellPositioningDescriptorEx defaultDescriptor = this.m_defaultDescriptor;
      for (int index = 0; index < this.m_initialRobWpList.Count; ++index)
      {
        KiAppCellPositioningDescriptorEx positioningDescriptor1 = ((KiAppWaypointEx) this.m_initialRobWpList[index]).BeforeWaypointCellPositioningDescriptor;
        this.UpdateDescriptorWithDefaultValues(positioningDescriptor1, defaultDescriptor);
        ((KiAppWaypointEx) this.m_initialRobWpList[index]).BeforeWaypointCellPositioningDescriptor = positioningDescriptor1;
        KiAppCellPositioningDescriptorEx positioningDescriptor2 = ((KiAppWaypointEx) this.m_initialRobWpList[index]).AfterWaypointCellPositioningDescriptor;
        this.UpdateDescriptorWithDefaultValues(positioningDescriptor2, defaultDescriptor);
        ((KiAppWaypointEx) this.m_initialRobWpList[index]).AfterWaypointCellPositioningDescriptor = positioningDescriptor2;
        defaultDescriptor = positioningDescriptor2;
      }
      this.KiTask = (KiAppTaskEx) new KiAppRoboticTaskEx(i_roboticOperation, (IList<KiAppRoboticWaypointEx>) this.m_initialRobWpList);
      this.SynchronizedWaypointList = this.Waypoints();
      TxApplication.ActiveDocument.SimulationPlayer.ResetToDefaultSetting();
    }
    TxApplication.RefreshDisplay();
  }

  private bool createWaypointsWithSimulation(
    ITxRoboticOrderedCompoundOperation i_roboticOperation)
  {
    bool waypointsWithSimulation = true;
    KiAppCellPositioningDescriptorEx positioningDescriptorEx = new KiAppCellPositioningDescriptorEx(this.m_defaultDescriptor);
    TxSimulationPlayer simulationPlayer = new TxSimulationPlayer(true, true);
    simulationPlayer.SetOperation(this.Operation);
    bool ignoreSignals = TxApplication.Options.Simulation.IgnoreSignals;
    TxSimulationPlayer.IgnoreSignals = true;
    bool stopOnCollision = TxApplication.Options.Collision.StopOnCollision;
    TxApplication.Options.Collision.StopOnCollision = false;
    simulationPlayer.IsAutomaticErrorOutputEnabled = false;
    simulationPlayer.OperationReachedDestination += new TxSimulationPlayer_OperationReachedDestinationEventHandler(this.mySimulator_OperationReachedDestination);
    simulationPlayer.OperationStartedForward += new TxSimulationPlayer_OperationStartedForwardEventHandler(this.mySimulator_OperationStartedForward);
    this.ResetAllZonesToFine((ITxRoboticOperation) i_roboticOperation);
    simulationPlayer.PlaySilently();
    simulationPlayer.Stop();
    simulationPlayer.OperationReachedDestination -= new TxSimulationPlayer_OperationReachedDestinationEventHandler(this.mySimulator_OperationReachedDestination);
    simulationPlayer.OperationStartedForward -= new TxSimulationPlayer_OperationStartedForwardEventHandler(this.mySimulator_OperationStartedForward);
    List<string> errorsAndTraces = simulationPlayer.GetErrorsAndTraces((TxSimulationErrorType) 256 /*0x0100*/);
    this.m_lastSimulationErrorMessages.Clear();
    if (errorsAndTraces.Count > 0)
    {
      this.m_lastSimulationErrorMessages = this.FilterIrrelevantErrorMessages(errorsAndTraces);
      if (this.m_lastSimulationErrorMessages.Count > 0)
      {
        TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations(this.Operation);
        if (this.m_lastReachedOperation != ((Collection<ITxObject>) operationLocations)[((Collection<ITxObject>) operationLocations).Count - 1])
          waypointsWithSimulation = false;
      }
    }
    simulationPlayer.Rewind();
    simulationPlayer.IsAutomaticErrorOutputEnabled = true;
    TxSimulationPlayer.IgnoreSignals = ignoreSignals;
    TxApplication.Options.Collision.StopOnCollision = stopOnCollision;
    return waypointsWithSimulation;
  }

  private void createWaypointFromCurrentLocation(
    ITxOperation location,
    ref KiAppRoboticWaypointEx waypoint)
  {
    TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations(this.Operation);
    this.CheckLocationStatus(location as ITxLocationOperation);
    int count = this.m_initialRobWpList.Count;
    if (location is ITxRoboticLocationOperation)
    {
      ITxRoboticLocationOperation locationOperation = location as ITxRoboticLocationOperation;
      KiAppRoboticLocationEx approachRoboticLocation = (KiAppRoboticLocationEx) null;
      KiAppRoboticLocationEx departRoboticLocation = (KiAppRoboticLocationEx) null;
      KiAppKinematicElementDescriptorEx kinematicElementDescriptor = (KiAppKinematicElementDescriptorEx) null;
      KiAppCellPositioningDescriptorEx cellDescriptor = (KiAppCellPositioningDescriptorEx) null;
      this.createAppRoboticLocationFromCurrentPostures((ITxRoboticOperation) locationOperation, ref approachRoboticLocation, ref departRoboticLocation, ref kinematicElementDescriptor, ref cellDescriptor);
      if (ITxRoboticLocationOperationEx.GetTaughtPose(locationOperation) != null)
      {
        approachRoboticLocation.RobotPose = ITxRoboticLocationOperationEx.GetTaughtPose(locationOperation);
        approachRoboticLocation.IsTaughtPose = true;
        if (departRoboticLocation != null)
        {
          departRoboticLocation.RobotPose = ITxRoboticLocationOperationEx.GetTaughtPose(locationOperation);
          departRoboticLocation.IsTaughtPose = true;
        }
      }
      approachRoboticLocation.IsTaughtConfiguration = locationOperation.RobotConfigurationData != null;
      if (departRoboticLocation != null)
        departRoboticLocation.IsTaughtConfiguration = locationOperation.RobotConfigurationData != null;
      TxMotionType locationMotionType = CApAPPRoboticTask.GetLocationMotionType(locationOperation);
      KiAppSpeedType locationSpeedType = CApAPPRoboticTask.GetLocationSpeedType(this.Robot, locationOperation);
      KiAppMotionType kiAppMotionType = locationMotionType == 2 ? (KiAppMotionType) 0 : (KiAppMotionType) 1;
      waypoint = new KiAppRoboticWaypointEx(approachRoboticLocation, departRoboticLocation, ((ITxObject) locationOperation).Name, false, (KiAppWaypointStatus) 0, kiAppMotionType);
      ((KiAppWaypointEx) waypoint).ZoneLabel = this.GetLocationZoneLabel(locationOperation);
      ((KiAppWaypointEx) waypoint).KinDescriptor = kinematicElementDescriptor;
      ((KiAppWaypointEx) waypoint).AfterWaypointCellPositioningDescriptor = cellDescriptor;
      ((KiAppWaypointEx) waypoint).BeforeWaypointCellPositioningDescriptor = this.m_previousDescriptor != null ? this.m_previousDescriptor : cellDescriptor;
      ((KiAppWaypointEx) waypoint).MotionSpeed = CApAPPRoboticTask.GetLocationMotionSpeed((ITxRoboticOperation) locationOperation, locationMotionType, locationSpeedType);
      ((KiAppWaypointEx) waypoint).SpeedType = locationSpeedType;
      ((KiAppWaypointEx) waypoint).IsWeld = location is TxWeldLocationOperation;
      ITxGun itxGun = (this.m_gunActiveWhenReachingLocation != null ? this.m_gunActiveWhenReachingLocation : this.GetActiveGun((ITxRobot) this.m_robot, this.m_robotControllerServices, locationOperation)) ?? this.m_previousValidActiveGun;
      ((KiAppWaypointEx) waypoint).BeforeWaypointActiveGun = itxGun;
      if (itxGun != null)
        this.m_previousValidActiveGun = itxGun;
    }
    else if (location is TxRoboticSeamOperation)
    {
      KiAppRoboticLocationEx approachRoboticLocation = (KiAppRoboticLocationEx) null;
      KiAppKinematicElementDescriptorEx kinematicElementDescriptor = (KiAppKinematicElementDescriptorEx) null;
      KiAppCellPositioningDescriptorEx cellDescriptor = (KiAppCellPositioningDescriptorEx) null;
      this.createAppRoboticLocationFromCurrentPostures(location as ITxRoboticOperation, false, ref approachRoboticLocation, ref kinematicElementDescriptor, ref cellDescriptor);
      approachRoboticLocation.IsTaughtPose = false;
      KiAppRoboticSeamLocationEx roboticSeamLocationEx = new KiAppRoboticSeamLocationEx();
      roboticSeamLocationEx.StartRobotPose = approachRoboticLocation.RobotPose;
      roboticSeamLocationEx.StartAuxiliaryDevicesPoses = approachRoboticLocation.AuxiliaryDevicesPoses;
      roboticSeamLocationEx.StartAuxiliaryDevices = approachRoboticLocation.AuxiliaryDevices;
      roboticSeamLocationEx.StartAbsoluteLocation = ((KiAppLocationEx) approachRoboticLocation).AbsoluteLocation;
      TxMotionType motionType = (TxMotionType) 2;
      KiAppSpeedType speedType = (KiAppSpeedType) 0;
      if ((location as TxRoboticSeamOperation).Count > 0)
      {
        motionType = CApAPPRoboticTask.GetLocationMotionType((location as TxRoboticSeamOperation).GetChildAt(0) as ITxRoboticLocationOperation);
        speedType = CApAPPRoboticTask.GetLocationSpeedType(this.Robot, (location as TxRoboticSeamOperation).GetChildAt(0) as ITxRoboticLocationOperation);
      }
      KiAppMotionType kiAppMotionType = motionType == 2 ? (KiAppMotionType) 0 : (KiAppMotionType) 1;
      waypoint = new KiAppRoboticWaypointEx((KiAppRoboticLocationEx) roboticSeamLocationEx, ((ITxObject) location).Name, false, (KiAppWaypointStatus) 0, kiAppMotionType);
      ((KiAppWaypointEx) waypoint).KinDescriptor = kinematicElementDescriptor;
      ((KiAppWaypointEx) waypoint).AfterWaypointCellPositioningDescriptor = cellDescriptor;
      ((KiAppWaypointEx) waypoint).BeforeWaypointCellPositioningDescriptor = this.m_previousDescriptor != null ? this.m_previousDescriptor : cellDescriptor;
      ((KiAppWaypointEx) waypoint).MotionSpeed = CApAPPRoboticTask.GetLocationMotionSpeed(location as ITxRoboticOperation, motionType, speedType);
      ((KiAppWaypointEx) waypoint).SpeedType = speedType;
      ITxGun itxGun = (this.m_gunActiveWhenReachingLocation != null ? this.m_gunActiveWhenReachingLocation : this.GetActiveGun((ITxRobot) this.m_robot, this.m_robotControllerServices, (ITxRoboticLocationOperation) null)) ?? this.m_previousValidActiveGun;
      ((KiAppWaypointEx) waypoint).BeforeWaypointActiveGun = itxGun;
      if (itxGun != null)
        this.m_previousValidActiveGun = itxGun;
    }
    if (waypoint == null)
      return;
    if (count == 0)
      ((KiAppWaypointEx) waypoint).Locked = true;
    else if (count == ((Collection<ITxObject>) operationLocations).Count - 1)
      ((KiAppWaypointEx) waypoint).Locked = true;
    if (!this.IsLockedLocation((ITxObject) location))
      return;
    ((KiAppWaypointEx) waypoint).Locked = true;
  }

  private void mySimulator_OperationStartedForward(
    object sender,
    TxSimulationPlayer_OperationStartedForwardEventArgs args)
  {
    try
    {
      if (this.m_shouldSkipNextOperationStartedForwardEvent)
      {
        this.m_shouldSkipNextOperationStartedForwardEvent = false;
      }
      else
      {
        ITxOperation operation = args.Operation;
        bool flag1 = true;
        TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations(this.Operation);
        ITxOperation itxOperation1 = (ITxOperation) null;
        ITxOperation itxOperation2 = (ITxOperation) null;
        if (this.m_initialRobWpList.Count > 0 && ((KiAppWaypointEx) this.m_initialRobWpList[this.m_initialRobWpList.Count - 1]).SimulatedApproachLocation is KiAppRoboticSeamLocationEx && (((KiAppWaypointEx) this.m_initialRobWpList[this.m_initialRobWpList.Count - 1]).SimulatedApproachLocation as KiAppRoboticSeamLocationEx).EndRobotPose.JointValues.Count == 0)
          flag1 = false;
        if (!flag1)
        {
          if (((Collection<ITxObject>) operationLocations).Count > this.m_initialRobWpList.Count)
          {
            itxOperation1 = ((Collection<ITxObject>) operationLocations)[this.m_initialRobWpList.Count - 1] as ITxOperation;
            itxOperation2 = ((Collection<ITxObject>) operationLocations)[this.m_initialRobWpList.Count] as ITxOperation;
          }
        }
        else
        {
          if (((Collection<ITxObject>) operationLocations).Count > this.m_initialRobWpList.Count)
            itxOperation1 = ((Collection<ITxObject>) operationLocations)[this.m_initialRobWpList.Count] as ITxOperation;
          if (!(itxOperation1 is TxRoboticSeamOperation) && ((Collection<ITxObject>) operationLocations).Count > this.m_initialRobWpList.Count + 1)
            itxOperation2 = ((Collection<ITxObject>) operationLocations)[this.m_initialRobWpList.Count + 1] as ITxOperation;
        }
        if (flag1 && itxOperation1 is TxRoboticSeamOperation)
        {
          TxRoboticSeamOperation roboticSeamOperation = itxOperation1 as TxRoboticSeamOperation;
          if (roboticSeamOperation.Count >= 2 && !roboticSeamOperation.Contains((ITxObject) operation))
            return;
          KiAppRoboticWaypointEx waypoint = (KiAppRoboticWaypointEx) null;
          this.createWaypointFromCurrentLocation(itxOperation1, ref waypoint);
          if (waypoint == null)
            return;
          this.m_initialRobWpList.Add(waypoint);
          if (this.m_initialZones == null || this.m_initialRobWpList.Count - 1 >= this.m_initialZones.Count || !(this.m_initialZones[this.m_initialRobWpList.Count - 1] is ArrayList) || (this.m_initialZones[this.m_initialRobWpList.Count - 1] as ArrayList).Count <= 0)
            return;
          ((KiAppWaypointEx) waypoint).ZoneLabel = (this.m_initialZones[this.m_initialRobWpList.Count - 1] as ArrayList)[0] as string;
        }
        else
        {
          bool flag2 = false;
          TxRoboticSeamOperation roboticSeamOperation = itxOperation2 as TxRoboticSeamOperation;
          if (TxRoboticSeamOperation.op_Inequality(roboticSeamOperation, (ITxObject) null) && roboticSeamOperation.Count > 0 && roboticSeamOperation.Contains((ITxObject) operation) || itxOperation2 == operation)
            flag2 = true;
          if (!flag2)
            return;
          if (itxOperation1 is TxRoboticSeamOperation)
          {
            KiAppRoboticSeamLocationEx approachLocation = ((KiAppWaypointEx) this.m_initialRobWpList[this.m_initialRobWpList.Count - 1]).SimulatedApproachLocation as KiAppRoboticSeamLocationEx;
            KiAppRoboticLocationEx approachRoboticLocation = (KiAppRoboticLocationEx) null;
            KiAppKinematicElementDescriptorEx kinematicElementDescriptor = (KiAppKinematicElementDescriptorEx) null;
            KiAppCellPositioningDescriptorEx cellDescriptor = (KiAppCellPositioningDescriptorEx) null;
            this.createAppRoboticLocationFromCurrentPostures(itxOperation1 as ITxRoboticOperation, false, ref approachRoboticLocation, ref kinematicElementDescriptor, ref cellDescriptor);
            if (approachLocation != null && approachRoboticLocation != null)
            {
              approachLocation.EndRobotPose = approachRoboticLocation.RobotPose;
              approachLocation.EndAuxiliaryDevicesPoses = approachRoboticLocation.AuxiliaryDevicesPoses;
              approachLocation.EndAuxiliaryDevices = approachRoboticLocation.AuxiliaryDevices;
              approachLocation.EndAbsoluteLocation = ((KiAppLocationEx) approachRoboticLocation).AbsoluteLocation;
            }
            ((KiAppWaypointEx) this.m_initialRobWpList[this.m_initialRobWpList.Count - 1]).SimulatedApproachLocation = (KiAppLocationEx) approachLocation;
          }
          else
          {
            KiAppRoboticWaypointEx waypoint = (KiAppRoboticWaypointEx) null;
            this.createWaypointFromCurrentLocation(itxOperation1, ref waypoint);
            if (waypoint == null)
              return;
            this.m_initialRobWpList.Add(waypoint);
            if (this.m_initialZones == null || this.m_initialRobWpList.Count - 1 >= this.m_initialZones.Count || !(this.m_initialZones[this.m_initialRobWpList.Count - 1] is ArrayList) || (this.m_initialZones[this.m_initialRobWpList.Count - 1] as ArrayList).Count <= 0)
              return;
            ((KiAppWaypointEx) waypoint).ZoneLabel = (this.m_initialZones[this.m_initialRobWpList.Count - 1] as ArrayList)[0] as string;
          }
        }
      }
    }
    catch (Exception ex)
    {
    }
  }

  private void mySimulator_OperationReachedDestination(
    object sender,
    TxSimulationPlayer_OperationReachedDestinationEventArgs args)
  {
    try
    {
      int count = this.m_initialRobWpList.Count;
      ITxOperation operation = args.Operation;
      if (this.m_lastReachedOperation == operation && CApAPPUtilities.CanUseRCSForRobot(this.Robot) && this.Robot.Controller.Name.Contains("Nachi") && !this.m_shouldSkipNextOperationStartedForwardEvent)
      {
        this.m_shouldSkipNextOperationStartedForwardEvent = true;
        this.FillDescriptorWithCurrentValues(this.m_previousDescriptor);
        this.m_gunActiveWhenReachingLocation = this.GetActiveGun((ITxRobot) this.m_robot, this.m_robotControllerServices, operation as ITxRoboticLocationOperation);
        KiAppRoboticWaypointEx waypoint = (KiAppRoboticWaypointEx) null;
        this.createWaypointFromCurrentLocation(operation, ref waypoint);
        TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations(this.Operation);
        if (waypoint == null)
          return;
        if (count == ((Collection<ITxObject>) operationLocations).Count && this.m_initialRobWpList.Count == ((Collection<ITxObject>) operationLocations).Count)
          this.m_initialRobWpList[this.m_initialRobWpList.Count - 1] = waypoint;
        else
          this.m_initialRobWpList.Add(waypoint);
        if (this.m_initialZones == null || this.m_initialRobWpList.Count - 1 >= this.m_initialZones.Count || !(this.m_initialZones[this.m_initialRobWpList.Count - 1] is ArrayList) || (this.m_initialZones[this.m_initialRobWpList.Count - 1] as ArrayList).Count <= 0)
          return;
        ((KiAppWaypointEx) waypoint).ZoneLabel = (this.m_initialZones[this.m_initialRobWpList.Count - 1] as ArrayList)[0] as string;
      }
      else
      {
        this.m_lastReachedOperation = operation;
        if (this.m_previousDescriptor == null)
          this.m_previousDescriptor = new KiAppCellPositioningDescriptorEx();
        this.FillDescriptorWithCurrentValues(this.m_previousDescriptor);
        this.m_gunActiveWhenReachingLocation = this.GetActiveGun((ITxRobot) this.m_robot, this.m_robotControllerServices, operation as ITxRoboticLocationOperation);
        TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations(this.Operation);
        if (((Collection<ITxObject>) operationLocations)[((Collection<ITxObject>) operationLocations).Count - 1] is TxRoboticSeamOperation || operation != ((Collection<ITxObject>) operationLocations)[((Collection<ITxObject>) operationLocations).Count - 1])
          return;
        KiAppRoboticWaypointEx waypoint = (KiAppRoboticWaypointEx) null;
        this.createWaypointFromCurrentLocation(operation, ref waypoint);
        if (waypoint == null)
          return;
        if (count == ((Collection<ITxObject>) operationLocations).Count && this.m_initialRobWpList.Count == ((Collection<ITxObject>) operationLocations).Count)
          this.m_initialRobWpList[this.m_initialRobWpList.Count - 1] = waypoint;
        else
          this.m_initialRobWpList.Add(waypoint);
        if (this.m_initialZones == null || this.m_initialRobWpList.Count - 1 >= this.m_initialZones.Count || !(this.m_initialZones[this.m_initialRobWpList.Count - 1] is ArrayList) || (this.m_initialZones[this.m_initialRobWpList.Count - 1] as ArrayList).Count <= 0)
          return;
        ((KiAppWaypointEx) waypoint).ZoneLabel = (this.m_initialZones[this.m_initialRobWpList.Count - 1] as ArrayList)[0] as string;
      }
    }
    catch (Exception ex)
    {
    }
  }

  private void createWaypointsWithJump(
    ITxRoboticOrderedCompoundOperation i_roboticOperation)
  {
    TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations((ITxOperation) i_roboticOperation);
    for (int index = 0; index < ((Collection<ITxObject>) operationLocations).Count; ++index)
    {
      ITxObject itxObject = ((Collection<ITxObject>) operationLocations)[index];
      if (itxObject is ITxRoboticLocationOperation)
      {
        KiAppRoboticLocationEx roboticLocation = (KiAppRoboticLocationEx) null;
        KiAppKinematicElementDescriptorEx kinElementDescriptor = (KiAppKinematicElementDescriptorEx) null;
        KiAppCellPositioningDescriptorEx cellDescriptor = (KiAppCellPositioningDescriptorEx) null;
        bool fromTxLocWithJump = this.createAppRoboticLocationFromTxLocWithJump(itxObject as ITxRoboticLocationOperation, ref roboticLocation, ref kinElementDescriptor, ref cellDescriptor, true);
        ITxGun itxGun = this.GetActiveGun((ITxRobot) this.m_robot, this.m_robotControllerServices, itxObject as ITxRoboticLocationOperation) ?? this.m_previousValidActiveGun;
        TxMotionType locationMotionType = CApAPPRoboticTask.GetLocationMotionType(itxObject as ITxRoboticLocationOperation);
        KiAppSpeedType locationSpeedType = CApAPPRoboticTask.GetLocationSpeedType(this.Robot, itxObject as ITxRoboticLocationOperation);
        KiAppMotionType kiAppMotionType = locationMotionType == 2 ? (KiAppMotionType) 0 : (KiAppMotionType) 1;
        KiAppRoboticWaypointEx roboticWaypointEx = new KiAppRoboticWaypointEx(roboticLocation, itxObject.Name, false, (KiAppWaypointStatus) 0, kiAppMotionType);
        ((KiAppWaypointEx) roboticWaypointEx).KinDescriptor = kinElementDescriptor;
        ((KiAppWaypointEx) roboticWaypointEx).AfterWaypointCellPositioningDescriptor = cellDescriptor;
        ((KiAppWaypointEx) roboticWaypointEx).BeforeWaypointCellPositioningDescriptor = cellDescriptor;
        ((KiAppWaypointEx) roboticWaypointEx).MotionSpeed = CApAPPRoboticTask.GetLocationMotionSpeed(itxObject as ITxRoboticOperation, locationMotionType, locationSpeedType);
        ((KiAppWaypointEx) roboticWaypointEx).SpeedType = locationSpeedType;
        ((KiAppWaypointEx) roboticWaypointEx).IsWeld = itxObject is TxWeldLocationOperation;
        ((KiAppWaypointEx) roboticWaypointEx).BeforeWaypointActiveGun = itxGun;
        if (itxGun != null)
          this.m_previousValidActiveGun = itxGun;
        if (this.m_initialZones != null && index < this.m_initialZones.Count && this.m_initialZones[index] is ArrayList && (this.m_initialZones[index] as ArrayList).Count > 0)
          ((KiAppWaypointEx) roboticWaypointEx).ZoneLabel = (this.m_initialZones[index] as ArrayList)[0] as string;
        else
          ((KiAppWaypointEx) roboticWaypointEx).ZoneLabel = this.GetLocationZoneLabel(itxObject as ITxRoboticLocationOperation);
        if (!fromTxLocWithJump)
          ((KiAppWaypointEx) roboticWaypointEx).WaypointStatus = (KiAppWaypointStatus) 7;
        this.m_initialRobWpList.Add(roboticWaypointEx);
      }
      else if (itxObject is TxRoboticSeamOperation)
      {
        KiAppRoboticSeamLocationEx o_roboticSeamLocation = (KiAppRoboticSeamLocationEx) null;
        KiAppKinematicElementDescriptorEx kinDescriptor = (KiAppKinematicElementDescriptorEx) null;
        KiAppCellPositioningDescriptorEx cellDescriptor = (KiAppCellPositioningDescriptorEx) null;
        bool operationWithJump = this.createAppSeamRoboticLocationFromTxSeamOperationWithJump(itxObject as TxRoboticSeamOperation, ref o_roboticSeamLocation, ref kinDescriptor, ref cellDescriptor, true);
        KiAppWaypointStatus appWaypointStatus = (KiAppWaypointStatus) 0;
        TxMotionType motionType = (TxMotionType) 2;
        KiAppSpeedType speedType = (KiAppSpeedType) 0;
        if ((itxObject as TxRoboticSeamOperation).Count > 0)
        {
          motionType = CApAPPRoboticTask.GetLocationMotionType((itxObject as TxRoboticSeamOperation).GetChildAt(0) as ITxRoboticLocationOperation);
          speedType = CApAPPRoboticTask.GetLocationSpeedType(this.Robot, (itxObject as TxRoboticSeamOperation).GetChildAt(0) as ITxRoboticLocationOperation);
        }
        KiAppMotionType kiAppMotionType = motionType == 2 ? (KiAppMotionType) 0 : (KiAppMotionType) 1;
        KiAppRoboticWaypointEx roboticWaypointEx = new KiAppRoboticWaypointEx((KiAppRoboticLocationEx) o_roboticSeamLocation, itxObject.Name, false, appWaypointStatus, kiAppMotionType);
        ((KiAppWaypointEx) roboticWaypointEx).KinDescriptor = kinDescriptor;
        ((KiAppWaypointEx) roboticWaypointEx).BeforeWaypointCellPositioningDescriptor = cellDescriptor;
        ((KiAppWaypointEx) roboticWaypointEx).AfterWaypointCellPositioningDescriptor = cellDescriptor;
        ((KiAppWaypointEx) roboticWaypointEx).MotionSpeed = CApAPPRoboticTask.GetLocationMotionSpeed(itxObject as ITxRoboticOperation, motionType, speedType);
        ((KiAppWaypointEx) roboticWaypointEx).SpeedType = speedType;
        if (!operationWithJump)
          ((KiAppWaypointEx) roboticWaypointEx).WaypointStatus = (KiAppWaypointStatus) 7;
        this.m_initialRobWpList.Add(roboticWaypointEx);
      }
      if (this.m_initialRobWpList[index] != null)
      {
        if (index == 0)
          ((KiAppWaypointEx) this.m_initialRobWpList[index]).Locked = true;
        else if (index == ((Collection<ITxObject>) operationLocations).Count - 1)
          ((KiAppWaypointEx) this.m_initialRobWpList[index]).Locked = true;
        if (this.IsLockedLocation(itxObject))
          ((KiAppWaypointEx) this.m_initialRobWpList[index]).Locked = true;
      }
    }
  }

  private bool configureWaypointsWithJump(
    ITxRoboticOrderedCompoundOperation i_roboticOperation)
  {
    TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations((ITxOperation) i_roboticOperation);
    bool flag = true;
    for (int index = 0; index < ((Collection<ITxObject>) operationLocations).Count & flag; ++index)
    {
      ITxObject itxObject = ((Collection<ITxObject>) operationLocations)[index];
      switch (itxObject)
      {
        case ITxRoboticLocationOperation _:
          KiAppRoboticLocationEx roboticLocation = (KiAppRoboticLocationEx) null;
          KiAppKinematicElementDescriptorEx kinElementDescriptor = (KiAppKinematicElementDescriptorEx) null;
          KiAppCellPositioningDescriptorEx cellDescriptor1 = (KiAppCellPositioningDescriptorEx) null;
          if (this.createAppRoboticLocationFromTxLocWithJump(itxObject as ITxRoboticLocationOperation, ref roboticLocation, ref kinElementDescriptor, ref cellDescriptor1, false))
          {
            if (index < this.m_initialRobWpList.Count)
            {
              ((KiAppWaypointEx) this.m_initialRobWpList[index]).StoredApproachLocation = (KiAppLocationEx) roboticLocation;
              break;
            }
            flag = false;
            break;
          }
          break;
        case TxRoboticSeamOperation _:
          KiAppRoboticSeamLocationEx o_roboticSeamLocation = (KiAppRoboticSeamLocationEx) null;
          KiAppKinematicElementDescriptorEx kinDescriptor = (KiAppKinematicElementDescriptorEx) null;
          KiAppCellPositioningDescriptorEx cellDescriptor2 = (KiAppCellPositioningDescriptorEx) null;
          if (this.createAppSeamRoboticLocationFromTxSeamOperationWithJump(itxObject as TxRoboticSeamOperation, ref o_roboticSeamLocation, ref kinDescriptor, ref cellDescriptor2, false))
          {
            if (index < this.m_initialRobWpList.Count)
            {
              ((KiAppWaypointEx) this.m_initialRobWpList[index]).StoredApproachLocation = (KiAppLocationEx) o_roboticSeamLocation;
              break;
            }
            flag = false;
            break;
          }
          break;
      }
    }
    return flag;
  }

  private bool createAppRoboticLocationFromTxLocWithJump(
    ITxRoboticLocationOperation i_location,
    ref KiAppRoboticLocationEx roboticLocation,
    ref KiAppKinematicElementDescriptorEx kinElementDescriptor,
    ref KiAppCellPositioningDescriptorEx cellDescriptor,
    bool hasSimulationAlreadyFailed)
  {
    bool location = this.JumpRobotToLocation(this.m_robot, i_location);
    this.CheckLocationStatus((ITxLocationOperation) i_location);
    this.createAppRoboticLocationFromCurrentPostures((ITxRoboticOperation) i_location, true, ref roboticLocation, ref kinElementDescriptor, ref cellDescriptor);
    if (!location && i_location is ITxLocatableObject)
    {
      ((KiAppLocationEx) roboticLocation).AbsoluteLocation = (i_location as ITxLocatableObject).AbsoluteLocation;
      if (hasSimulationAlreadyFailed)
        this.m_nonReachableElements.Add((ITxLocationOperation) i_location);
    }
    if (ITxRoboticLocationOperationEx.GetTaughtPose(i_location) != null)
    {
      roboticLocation.RobotPose = ITxRoboticLocationOperationEx.GetTaughtPose(i_location);
      roboticLocation.IsTaughtPose = true;
    }
    if (i_location.RobotConfigurationData != null)
      roboticLocation.IsTaughtConfiguration = true;
    return location;
  }

  private void createAppRoboticLocationFromCurrentPostures(
    ITxRoboticOperation locationOperation,
    bool isJump,
    ref KiAppRoboticLocationEx approachRoboticLocation,
    ref KiAppKinematicElementDescriptorEx kinematicElementDescriptor,
    ref KiAppCellPositioningDescriptorEx cellDescriptor)
  {
    approachRoboticLocation = new KiAppRoboticLocationEx();
    if ((this.Operation as ITxRoboticOperation).IsMountedWorkpieceOperation && TxFrame.op_Inequality(this.GetLocationRemoteTCPF((ITxRobot) this.m_robot, this.m_robotControllerServices, this.Operation as ITxRoboticOperation, locationOperation as ITxRoboticLocationOperation), (ITxObject) null))
    {
      if (locationOperation is ITxLocatableObject)
        ((KiAppLocationEx) approachRoboticLocation).AbsoluteLocation = (locationOperation as ITxLocatableObject).AbsoluteLocation;
      else
        ((KiAppLocationEx) approachRoboticLocation).AbsoluteLocation = this.GetLocationRemoteTCPF((ITxRobot) this.m_robot, this.m_robotControllerServices, this.Operation as ITxRoboticOperation, locationOperation as ITxRoboticLocationOperation).AbsoluteLocation;
    }
    else if (isJump && locationOperation is ITxLocatableObject)
      ((KiAppLocationEx) approachRoboticLocation).AbsoluteLocation = (locationOperation as ITxLocatableObject).AbsoluteLocation;
    else
      ((KiAppLocationEx) approachRoboticLocation).AbsoluteLocation = this.m_robot.TCPF.AbsoluteLocation;
    kinematicElementDescriptor = KiAppKinematicElementDescriptorEx.CreateKinElementDescriptorHierarchy(this.Operation);
    cellDescriptor = new KiAppCellPositioningDescriptorEx();
    TxObjectList<ITxDevice> handledDevices = kinematicElementDescriptor.HandledDevices;
    ((Collection<ITxDevice>) handledDevices).Remove((ITxDevice) this.m_robot);
    approachRoboticLocation.RobotPose = this.m_robot.CurrentPose;
    approachRoboticLocation.IsTaughtPose = false;
    approachRoboticLocation.IsTaughtConfiguration = false;
    approachRoboticLocation.RemoteTCPF = this.GetLocationRemoteTCPF((ITxRobot) this.m_robot, this.m_robotControllerServices, this.Operation as ITxRoboticOperation, locationOperation as ITxRoboticLocationOperation);
    TxObjectList<ITxLocatableObject> locatableObjects = kinematicElementDescriptor.HandledLocatableObjects;
    if (TxFrame.op_Inequality(approachRoboticLocation.RemoteTCPF, (ITxObject) null) && locatableObjects != null)
      ((Collection<ITxLocatableObject>) locatableObjects).Add((ITxLocatableObject) approachRoboticLocation.RemoteTCPF);
    this.AddHandledDevices(kinematicElementDescriptor.HandledDevices);
    this.AddHandledLocatableObjects(locatableObjects);
    this.FillDescriptorWithCurrentValues(cellDescriptor);
    List<TxPoseData> txPoseDataList = new List<TxPoseData>();
    for (int index = 0; index < ((Collection<ITxDevice>) handledDevices).Count; ++index)
      txPoseDataList.Add(((Collection<ITxDevice>) handledDevices)[index].CurrentPose);
    approachRoboticLocation.AuxiliaryDevices = handledDevices;
    approachRoboticLocation.AuxiliaryDevicesPoses = txPoseDataList;
  }

  private void createAppRoboticLocationFromCurrentPostures(
    ITxRoboticOperation locationOperation,
    ref KiAppRoboticLocationEx approachRoboticLocation,
    ref KiAppRoboticLocationEx departRoboticLocation,
    ref KiAppKinematicElementDescriptorEx kinematicElementDescriptor,
    ref KiAppCellPositioningDescriptorEx cellDescriptor)
  {
    this.createAppRoboticLocationFromCurrentPostures(locationOperation, false, ref approachRoboticLocation, ref kinematicElementDescriptor, ref cellDescriptor);
    ITxRoboticLocationOperation locationOperation1 = locationOperation as ITxRoboticLocationOperation;
    if (locationOperation1.RobotDepartureExternalAxesData == null)
      return;
    departRoboticLocation = new KiAppRoboticLocationEx(approachRoboticLocation);
    TxRobotExternalAxisData[] externalAxesData = locationOperation1.RobotDepartureExternalAxesData;
    for (int index1 = 0; index1 < departRoboticLocation.AuxiliaryDevicesPoses.Count; ++index1)
    {
      for (int index2 = 0; index2 < externalAxesData.Length; ++index2)
      {
        if (((Collection<ITxDevice>) departRoboticLocation.AuxiliaryDevices)[index1] == externalAxesData[index2].Device)
        {
          departRoboticLocation.AuxiliaryDevicesPoses[index1].JointValues[0] = (object) externalAxesData[index2].JointValue;
          break;
        }
      }
    }
  }

  private bool createAppSeamRoboticLocationFromTxSeamOperationWithJump(
    TxRoboticSeamOperation i_seamOperation,
    ref KiAppRoboticSeamLocationEx o_roboticSeamLocation,
    ref KiAppKinematicElementDescriptorEx kinDescriptor,
    ref KiAppCellPositioningDescriptorEx cellDescriptor,
    bool hasSimulationAlreadyFailed)
  {
    KiAppRoboticSeamLocationEx roboticSeamLocationEx = new KiAppRoboticSeamLocationEx();
    bool flag1 = true;
    bool flag2 = true;
    if (i_seamOperation.Count > 0)
    {
      ITxRoboticLocationOperation childAt1 = i_seamOperation.GetChildAt(0) as ITxRoboticLocationOperation;
      ITxRoboticLocationOperation childAt2 = i_seamOperation.GetChildAt(i_seamOperation.Count - 1) as ITxRoboticLocationOperation;
      KiAppRoboticLocationEx roboticLocation1 = (KiAppRoboticLocationEx) null;
      KiAppRoboticLocationEx roboticLocation2 = (KiAppRoboticLocationEx) null;
      flag1 = this.createAppRoboticLocationFromTxLocWithJump(childAt1, ref roboticLocation1, ref kinDescriptor, ref cellDescriptor, hasSimulationAlreadyFailed);
      KiAppKinematicElementDescriptorEx kinElementDescriptor = (KiAppKinematicElementDescriptorEx) null;
      KiAppCellPositioningDescriptorEx cellDescriptor1 = (KiAppCellPositioningDescriptorEx) null;
      flag2 = this.createAppRoboticLocationFromTxLocWithJump(childAt2, ref roboticLocation2, ref kinElementDescriptor, ref cellDescriptor1, hasSimulationAlreadyFailed);
      roboticSeamLocationEx.StartRobotPose = roboticLocation1.RobotPose;
      roboticSeamLocationEx.StartAuxiliaryDevicesPoses = roboticLocation1.AuxiliaryDevicesPoses;
      roboticSeamLocationEx.StartAuxiliaryDevices = roboticLocation1.AuxiliaryDevices;
      roboticSeamLocationEx.StartAbsoluteLocation = ((KiAppLocationEx) roboticLocation1).AbsoluteLocation;
      roboticSeamLocationEx.EndRobotPose = roboticLocation2.RobotPose;
      roboticSeamLocationEx.EndAuxiliaryDevicesPoses = roboticLocation2.AuxiliaryDevicesPoses;
      roboticSeamLocationEx.EndAuxiliaryDevices = roboticLocation2.AuxiliaryDevices;
      roboticSeamLocationEx.EndAbsoluteLocation = ((KiAppLocationEx) roboticLocation2).AbsoluteLocation;
    }
    o_roboticSeamLocation = roboticSeamLocationEx;
    return flag1 & flag2;
  }

  public bool JumpRobotToLocation(TxRobot i_robot, ITxRoboticLocationOperation i_loc)
  {
    TxPoseData txPoseData = (TxPoseData) null;
    TxJumpToLocationData jumpToLocationData = new TxJumpToLocationData();
    jumpToLocationData.ForceConfiguration = false;
    jumpToLocationData.UseWorkToolAndSystemFrames = true;
    jumpToLocationData.UseExternalAxes = true;
    jumpToLocationData.UseTaughtLocations = true;
    jumpToLocationData.UseTaughtPose = false;
    jumpToLocationData.GenerateMessage = false;
    TxJumpToLocationStatus toLocationStatus = (TxJumpToLocationStatus) 2;
    TxOlpControllerUtilities controllerUtilities = new TxOlpControllerUtilities();
    ITxRoboticControllerServices controllerServices = this.m_robotControllerServices;
    if (controllerServices != null)
    {
      TxRobotInverseData.TxInverseType txInverseType = (TxRobotInverseData.TxInverseType) 0;
      controllerServices.Init(i_robot);
      toLocationStatus = controllerServices.CheckInverse((ITxLocationOperation) i_loc, jumpToLocationData, txInverseType, ref txPoseData);
      if (toLocationStatus == null || toLocationStatus == 1)
      {
        i_robot.CurrentPose = txPoseData;
        if (i_loc.RobotExternalAxesData != null)
        {
          for (int index = 0; index < i_loc.RobotExternalAxesData.Length; ++index)
            i_loc.RobotExternalAxesData[index].Joint.CurrentValue = i_loc.RobotExternalAxesData[index].JointValue;
        }
      }
    }
    return toLocationStatus == null || toLocationStatus == 1;
  }

  public bool IsJointProfileValidForAPP => this.m_isJointProfileValidForAPP;

  public bool IsRobotConfigValidForApp => this.m_isRobotConfigValidForApp;

  private void CheckIsJointProfileValid()
  {
    this.m_isJointProfileValidForAPP = false;
    ArrayList motionData = TxDeviceEx.GetMotionData((ITxDevice) this.m_robot);
    if (motionData == null)
      return;
    Regex regex1 = new Regex("^(single_joint_prof)\\s+(single_prof)\\s*;");
    Regex regex2 = new Regex("^(joint_vel_prof)\\s+(all)\\s+(rect_prof)\\s*;");
    foreach (string input in motionData)
    {
      if (regex1.Match(input).Success || regex2.Match(input).Success)
      {
        this.m_isJointProfileValidForAPP = true;
        break;
      }
    }
  }

  private void CheckIsRobotConfigValidForAPP()
  {
    this.m_isRobotConfigValidForApp = false;
    if (((Collection<ITxObject>) this.m_robot.DrivingJoints).Count != 6)
    {
      this.m_isRobotConfigValidForApp = true;
    }
    else
    {
      TxPoseData currentPose = this.m_robot.CurrentPose;
      ArrayList jointValues = currentPose.JointValues;
      for (int index = 0; index < ((Collection<ITxObject>) this.m_robot.DrivingJoints).Count; ++index)
      {
        TxJoint drivingJoint = ((Collection<ITxObject>) this.m_robot.DrivingJoints)[index] as TxJoint;
        jointValues[index] = (object) (drivingJoint.LowerSoftLimit + (drivingJoint.UpperSoftLimit - drivingJoint.LowerSoftLimit) * 0.1);
      }
      currentPose.JointValues = jointValues;
      TxRobotConfigurationData poseConfiguration = this.m_robot.GetPoseConfiguration(currentPose);
      if (poseConfiguration.JointsConfigurations.Count != 6)
        return;
      if ((poseConfiguration.JointsConfigurations[2] as TxJointConfigurationData).State != 1 && (poseConfiguration.JointsConfigurations[4] as TxJointConfigurationData).State != 1 && poseConfiguration.OverheadState != 1)
        this.m_isRobotConfigValidForApp = true;
      if ((poseConfiguration.JointsConfigurations[2] as TxJointConfigurationData).State == 1 || (poseConfiguration.JointsConfigurations[3] as TxJointConfigurationData).State == 1 || poseConfiguration.OverheadState == 1)
        return;
      this.m_isRobotConfigValidForApp = true;
    }
  }

  private void CheckLocationNonProjectedStatus(ITxLocationOperation i_location)
  {
    if (!(i_location is TxWeldLocationOperation) || (i_location as TxWeldLocationOperation).IsProjected || this.m_nonProjectedElements.Contains(i_location))
      return;
    this.m_nonProjectedElements.Add(i_location);
  }

  private void CheckLocationOLPCommandsStatus(ITxLocationOperation i_location)
  {
    if (!(i_location is ITxRoboticOperation) || this.IsLockedLocation((ITxObject) i_location) || ((Collection<ITxObject>) (i_location as ITxRoboticOperation).Commands).Count <= 0 || this.m_olpCommandsElements.Contains(i_location))
      return;
    this.m_olpCommandsElements.Add(i_location);
  }

  public List<string> GetNotLockedAndNotFixedOLPCommandsLocationNames()
  {
    List<string> commandsLocationNames = new List<string>();
    TxObjectList allLocations = this.GetAllLocations();
    for (int index = 0; index < ((Collection<ITxObject>) allLocations).Count; ++index)
    {
      CApAPPWaypoint capAppWaypoint = this.Waypoint(index);
      if (!capAppWaypoint.Locked && !capAppWaypoint.Fixed)
      {
        ITxObject i_location = ((Collection<ITxObject>) allLocations)[index];
        if (i_location is ITxRoboticOperation && !this.IsLockedLocation(i_location) && ((Collection<ITxObject>) (i_location as ITxRoboticOperation).Commands).Count > 0)
          commandsLocationNames.Add($"{((ITxObject) this.Operation).Name}: {i_location.Name}");
      }
    }
    return commandsLocationNames;
  }

  public List<string> NonProjectedLocationNames
  {
    get
    {
      List<string> projectedLocationNames = new List<string>();
      foreach (ITxObject projectedLocation in this.NonProjectedLocations)
      {
        string str = $"{((ITxObject) this.Operation).Name}: {projectedLocation.Name}";
        projectedLocationNames.Add(str);
      }
      return projectedLocationNames;
    }
  }

  public List<ITxLocationOperation> NonProjectedLocations => this.m_nonProjectedElements;

  public List<string> OLPCommandsLocationNames
  {
    get
    {
      List<string> commandsLocationNames = new List<string>();
      foreach (ITxObject commandsLocation in this.OLPCommandsLocations)
      {
        string str = $"{((ITxObject) this.Operation).Name}: {commandsLocation.Name}";
        commandsLocationNames.Add(str);
      }
      return commandsLocationNames;
    }
  }

  public List<ITxLocationOperation> OLPCommandsLocations => this.m_olpCommandsElements;

  public List<string> NonReachableCommandsNames
  {
    get
    {
      List<string> reachableCommandsNames = new List<string>();
      foreach (ITxObject commandsLocation in this.NonReachableCommandsLocations)
      {
        string str = $"{((ITxObject) this.Operation).Name}: {commandsLocation.Name}";
        reachableCommandsNames.Add(str);
      }
      return reachableCommandsNames;
    }
  }

  public List<ITxLocationOperation> NonReachableCommandsLocations => this.m_nonReachableElements;

  public bool AreWaypointsCreatedWithJump => this.m_areWaypointCreatedWithJump;

  public List<string> LastSimulationErrorMessages => this.m_lastSimulationErrorMessages;

  private void CheckLocationStatus(ITxLocationOperation i_location)
  {
    this.CheckLocationNonProjectedStatus(i_location);
    this.CheckLocationOLPCommandsStatus(i_location);
  }

  protected override bool IsLockedLocation(ITxObject i_location)
  {
    bool flag = false;
    switch (i_location)
    {
      case TxWeldLocationOperation _:
      case TxRoboticSeamOperation _:
        flag = true;
        break;
    }
    TxObjectList commands = (i_location as ITxRoboticOperation).Commands;
    if (i_location is ITxRoboticOperation && ((Collection<ITxObject>) (i_location as ITxRoboticOperation).Commands).Count > 0 && this.AreAllOLPCommandsSupported((i_location as ITxRoboticOperation).Commands))
      flag = true;
    return flag;
  }

  private ITxGun GetActiveMountedGun(ITxRobot robot)
  {
    ITxGun activeMountedGun = (ITxGun) null;
    for (int index = 0; index < ((Collection<ITxObject>) robot.MountedTools).Count; ++index)
    {
      if (((Collection<ITxObject>) robot.MountedTools)[index] is ITxGun)
      {
        activeMountedGun = ((Collection<ITxObject>) robot.MountedTools)[index] as ITxGun;
        break;
      }
    }
    return activeMountedGun;
  }

  private ITxGun GetRCSGun(
    ITxRobot robot,
    ITxRoboticControllerServices robotControllerServices,
    ITxRoboticLocationOperation location)
  {
    ITxGun rcsGun = (ITxGun) null;
    if (location != null && robotControllerServices != null)
      rcsGun = robotControllerServices.GetActiveGun((ITxLocationOperation) location);
    return rcsGun;
  }

  private ITxGun GetActiveGun(
    ITxRobot robot,
    ITxRoboticControllerServices robotControllerServices,
    ITxRoboticLocationOperation location)
  {
    ITxGun itxGun = (ITxGun) null;
    ITxGun activeGun = this.GetActiveMountedGun(robot);
    if (activeGun != null && (activeGun as ITxDisplayableObject).Visibility == 2)
    {
      itxGun = activeGun;
      activeGun = (ITxGun) null;
    }
    if (activeGun == null)
      activeGun = this.GetRCSGun(robot, robotControllerServices, location);
    if (activeGun == null)
      activeGun = itxGun;
    return activeGun;
  }

  private ITxRoboticControllerServices GetRoboticControllerServices(ITxRobot robot)
  {
    ITxRoboticControllerServices controllerServices = (ITxRoboticControllerServices) null;
    if (robot != null && robot is TxRobot)
    {
      TxRobotController controller = robot.Controller;
      if (controller != null)
      {
        TxOlpControllerUtilities controllerUtilities = new TxOlpControllerUtilities();
        controllerServices = (ITxRoboticControllerServices) controllerUtilities.GetInterfaceImplementationFromController(controller.Name, typeof (ITxRoboticControllerServices), typeof (TxControllerAttribute), "ControllerName") ?? (ITxRoboticControllerServices) controllerUtilities.GetInterfaceImplementationFromController("default", typeof (ITxRoboticControllerServices), typeof (TxControllerAttribute), "ControllerName");
        controllerServices?.Init(robot as TxRobot);
      }
    }
    return controllerServices;
  }

  private List<string> FilterIrrelevantErrorMessages(List<string> errorMessages)
  {
    List<string> stringList = new List<string>();
    foreach (string errorMessage in errorMessages)
    {
      if (!errorMessage.Contains("Motion forced to full arrival") && !errorMessage.Contains("Zone forced to fine") && !errorMessage.Contains("Zone nach Fine umgestellt") && !errorMessage.Contains("Anfahrpunkt wird") && !errorMessage.Contains("fine afin d'effectuer une action sur la location") && !errorMessage.Contains("fine sur la derniere location") && !errorMessage.Contains("utilisation de la zone par d?faut fine") && errorMessage.Contains("[Error]"))
        stringList.Add(errorMessage);
    }
    return stringList;
  }

  public bool IsRedundantSystemTask() => this.KiRoboticTask.IsRedundantSystemTask();

  public override void RestoreInitialState()
  {
    this.UpdateStateFromDescriptor(this.m_defaultDescriptor);
    TxApplication.RefreshDisplay();
  }

  public override void UpdateApproachDepartTxOperation(
    List<CApAPPWaypoint> initialWaypoint,
    bool i_isUndo = false)
  {
    TxApplicationEx.FireDocumentUpdateStarted();
    this.UpdateSynchronizedWaypointList();
    ITxObject location = this.GetLocation(0);
    int index1 = 0;
    for (int index2 = 0; index2 < this.Waypoints().Count; ++index2)
    {
      if (index1 >= initialWaypoint.Count || !this.Waypoint(index2).IsSimular(initialWaypoint[index1]))
      {
        if (index1 < initialWaypoint.Count && this.IsSpecifiedWaypointSimilarToAnotherWaypointFromIndex(initialWaypoint[index1], this.Waypoints(), index2))
        {
          this.AddLocationFromWaypoint(this.Waypoint(index2), location);
          if (index2 == 0 && index2 < this.CountWaypoints())
            ((ITxOrderedObjectCollection) (this.Operation as ITxRoboticOrderedCompoundOperation)).MoveChildAfter(location, this.GetLocation(index2 + 1));
        }
        else
        {
          ++index1;
          this.GetLocation(index2).Delete();
          --index2;
        }
      }
      else
      {
        ++index1;
        this.UpdateLocationFromWaypoint(this.Waypoint(index2), this.GetLocation(index2));
      }
      location = this.GetLocation(index2);
    }
    TxApplicationEx.FireDocumentUpdateEnded();
  }

  private bool IsSpecifiedWaypointSimilarToAnotherWaypointFromIndex(
    CApAPPWaypoint waypointToLook,
    List<CApAPPWaypoint> listToSearch,
    int startIndex)
  {
    bool waypointFromIndex = false;
    for (int index = startIndex + 1; index < listToSearch.Count && !waypointFromIndex; ++index)
    {
      if (listToSearch[index].IsSimular(waypointToLook))
        waypointFromIndex = true;
    }
    return waypointFromIndex;
  }

  private void UpdateAllLocationsRRSFrame()
  {
    int o_firstIndex = -1;
    int o_lastIndex = -1;
    this.GetActiveRange(ref o_firstIndex, ref o_lastIndex);
    TxObjectList allLocations = this.GetAllLocations();
    if (o_firstIndex == -1 || o_lastIndex == -1 || o_lastIndex >= ((Collection<ITxObject>) allLocations).Count)
      return;
    TxFrame frame = (TxFrame) null;
    for (int index = o_lastIndex; index >= o_firstIndex; --index)
    {
      if (this.Waypoint(index).Fixed)
        frame = this.GetLocationRobotTCPF(((Collection<ITxObject>) allLocations)[index] as ITxRoboticLocationOperation);
      else if (TxFrame.op_Inequality(frame, (ITxObject) null))
        this.SetLocationRobotTCPF(((Collection<ITxObject>) allLocations)[index] as ITxRoboticLocationOperation, frame);
    }
  }

  public override void UpdateTxOperation(bool i_isUndo = false)
  {
    using (new CApAPPViewerDisabler())
    {
      base.UpdateTxOperation(i_isUndo);
      TxObjectList allLocations = this.GetAllLocations();
      if (((Collection<ITxObject>) allLocations).Count > 0)
      {
        TxMotionType motionType1 = this.Waypoint(0).TargetType == CApAPPMotionType.LinearMotion ? (TxMotionType) 2 : (TxMotionType) 1;
        this.SetLocationMotionType(((Collection<ITxObject>) allLocations)[0] as ITxRoboticOperation, motionType1);
        this.SetLocationMotionSpeed(((Collection<ITxObject>) allLocations)[0] as ITxRoboticOperation, motionType1, this.Waypoint(0).KiWaypoint.SpeedType, this.Waypoint(0).MotionSpeed);
        this.SetLocationSpeedType(((Collection<ITxObject>) allLocations)[0] as ITxRoboticOperation, this.Waypoint(0).KiWaypoint.SpeedType);
        if (this.CountWaypoints() > ((Collection<ITxObject>) allLocations).Count - 1 && ((Collection<ITxObject>) allLocations)[((Collection<ITxObject>) allLocations).Count - 1] is ITxRoboticLocationOperation)
        {
          TxMotionType motionType2 = this.Waypoint(((Collection<ITxObject>) allLocations).Count - 1).TargetType == CApAPPMotionType.LinearMotion ? (TxMotionType) 2 : (TxMotionType) 1;
          this.SetLocationMotionType(((Collection<ITxObject>) allLocations)[((Collection<ITxObject>) allLocations).Count - 1] as ITxRoboticOperation, motionType2);
          this.SetLocationMotionSpeed(((Collection<ITxObject>) allLocations)[((Collection<ITxObject>) allLocations).Count - 1] as ITxRoboticOperation, motionType2, this.Waypoint(((Collection<ITxObject>) allLocations).Count - 1).KiWaypoint.SpeedType, this.Waypoint(((Collection<ITxObject>) allLocations).Count - 1).MotionSpeed);
          this.SetLocationSpeedType(((Collection<ITxObject>) allLocations)[((Collection<ITxObject>) allLocations).Count - 1] as ITxRoboticOperation, this.Waypoint(((Collection<ITxObject>) allLocations).Count - 1).KiWaypoint.SpeedType);
        }
      }
      this.UpdateAllLocationsRRSFrame();
      this.m_nonProjectedElements.Clear();
      this.m_olpCommandsElements.Clear();
      for (int index = 0; index < ((Collection<ITxObject>) allLocations).Count; ++index)
      {
        if (((Collection<ITxObject>) allLocations)[index] is ITxLocationOperation)
          this.CheckLocationStatus(((Collection<ITxObject>) allLocations)[index] as ITxLocationOperation);
      }
      if (!i_isUndo)
        return;
      this.SetOperationZones(this.Operation as ITxRoboticOperation, this.m_initialZones);
      this.SetOperationParameters(this.Operation as ITxRoboticOperation, this.m_origLocationParameters);
    }
  }

  public virtual void UpdateTxOperationZones()
  {
    TxApplicationEx.FireDocumentUpdateStarted();
    TxObjectList allLocations = this.GetAllLocations();
    int o_firstIndex = -1;
    int o_lastIndex = -1;
    this.GetActiveRange(ref o_firstIndex, ref o_lastIndex);
    if (o_firstIndex != -1 && o_lastIndex != -1 && o_lastIndex < ((Collection<ITxObject>) allLocations).Count)
    {
      for (int index = o_firstIndex; index <= o_lastIndex; ++index)
      {
        CApAPPWaypoint capAppWaypoint = this.Waypoint(index);
        if (!capAppWaypoint.Fixed)
        {
          TxMotionType txMotionType = capAppWaypoint.MotionType == CApAPPMotionType.LinearMotion ? (TxMotionType) 2 : (TxMotionType) 1;
          this.SetLocationZone(((Collection<ITxObject>) allLocations)[index] as ITxRoboticOperation, capAppWaypoint, txMotionType);
          this.UpdateWaypointZoneLabel(capAppWaypoint, txMotionType);
          if (capAppWaypoint.MotionSpeed > 0.0)
          {
            this.SetLocationMotionSpeed(((Collection<ITxObject>) allLocations)[index] as ITxRoboticOperation, txMotionType, capAppWaypoint.KiWaypoint.SpeedType, capAppWaypoint.MotionSpeed);
            this.SetLocationSpeedType(((Collection<ITxObject>) allLocations)[index] as ITxRoboticOperation, capAppWaypoint.KiWaypoint.SpeedType);
          }
        }
      }
    }
    this.UpdateSynchronizedWaypointList();
    TxApplicationEx.FireDocumentUpdateEnded();
  }

  public override void UpdateExternalAxis(CApAPPWaypoint i_waypoint, ITxObject i_location)
  {
    TxWeldLocationOperation locationOperation = i_location as TxWeldLocationOperation;
    ITxRoboticLocationOperation operation = i_location as ITxRoboticLocationOperation;
    if (!(operation is TxRoboticViaLocationOperation) || operation.RobotExternalAxesData == null)
      return;
    if (i_waypoint.StoredDepartLocation != null && i_waypoint.StoredDepartLocation is KiAppRoboticLocationEx storedDepartLocation && storedDepartLocation.AuxiliaryDevicesPoses.Count > 0)
      this.SetDepartExternalDevicesPoses(operation, storedDepartLocation.AuxiliaryDevices, storedDepartLocation.AuxiliaryDevicesPoses);
    if (!(i_waypoint.StoredApproachLocation is KiAppRoboticLocationEx approachLocation) || approachLocation.AuxiliaryDevicesPoses.Count <= 0)
      return;
    this.SetApproachExternalDevicesPoses(operation, approachLocation.AuxiliaryDevices, approachLocation.AuxiliaryDevicesPoses);
  }

  public override void UpdateLocationFromWaypoint(CApAPPWaypoint i_waypoint, ITxObject i_location)
  {
    ITxRoboticOrderedCompoundOperation operation = this.Operation as ITxRoboticOrderedCompoundOperation;
    if (i_waypoint == null)
      return;
    ITxRoboticLocationOperation locationOperation = i_location as ITxRoboticLocationOperation;
    if (locationOperation is TxWeldLocationOperation || locationOperation is TxRoboticViaLocationOperation)
    {
      if (!(i_waypoint.SimulatedApproachLocation is KiAppRoboticLocationEx approachLocation))
        approachLocation = i_waypoint.StoredApproachLocation as KiAppRoboticLocationEx;
      TxRobotConfigurationData poseConfiguration = this.Robot.GetPoseConfiguration(approachLocation.RobotPose);
      locationOperation.RobotConfigurationData = !approachLocation.IsTaughtConfiguration || !CApAPPRoboticTask.IsConfigDataValid(this.Robot.GetPoseConfiguration(approachLocation.RobotPose)) ? (TxRobotConfigurationData) null : poseConfiguration;
      this.SetMotionAndSpeedData((ITxRoboticOperation) locationOperation, i_waypoint);
      if (this.IsRedundantSystemTask())
      {
        if (approachLocation.IsTaughtPose)
          ITxRoboticLocationOperationEx.SetTaughtPose(locationOperation, approachLocation.RobotPose);
        else
          ITxRoboticLocationOperationEx.SetTaughtPose(locationOperation, (TxPoseData) null);
      }
    }
    if (i_waypoint.StoredApproachLocation != null && i_waypoint.StoredApproachLocation is KiAppRoboticLocationEx approachLocation1 && locationOperation is TxRoboticViaLocationOperation && (locationOperation.RobotExternalAxesData == null || locationOperation.RobotExternalAxesData.Length == 0) && approachLocation1.AuxiliaryDevicesPoses.Count > 0)
      this.SetApproachExternalDevicesPoses((ITxRoboticLocationOperation) (locationOperation as TxRoboticViaLocationOperation), approachLocation1.AuxiliaryDevices, approachLocation1.AuxiliaryDevicesPoses);
    if (locationOperation == null || !i_waypoint.Fixed)
      return;
    TxMotionType motionType = i_waypoint.MotionType == CApAPPMotionType.LinearMotion ? (TxMotionType) 2 : (TxMotionType) 1;
    this.SetLocationZone((ITxRoboticOperation) locationOperation, i_waypoint, motionType);
  }

  public override void AddLocationFromWaypoint(
    CApAPPWaypoint i_waypoint,
    ITxObject i_previousLocation)
  {
    if (i_waypoint == null || !(i_waypoint.StoredApproachLocation is KiAppRoboticLocationEx approachLocation))
      return;
    TxRoboticViaLocationOperation locationOperation = this.CreateViaLocationOperation(approachLocation);
    if (!TxRoboticViaLocationOperation.op_Inequality(locationOperation, (ITxObject) null))
      return;
    ITxGun itxGun = (ITxGun) null;
    ITxRoboticControllerServices controllerServices = this.m_robotControllerServices;
    if (controllerServices != null && this.GetFollowingLocation(i_previousLocation) is ITxRoboticLocationOperation)
      itxGun = controllerServices.GetActiveGun((ITxLocationOperation) (this.GetFollowingLocation(i_previousLocation) as ITxRoboticLocationOperation));
    ITxRoboticLocationOperation i_locToCopy = itxGun != null ? this.GetFollowingLocation(i_previousLocation) as ITxRoboticLocationOperation : i_previousLocation as ITxRoboticLocationOperation;
    if (i_locToCopy != null)
      this.FillLocationParameter(locationOperation, i_locToCopy);
    this.UpdateLocationParametersFromWaypoint(locationOperation, i_waypoint);
    if (TxFrame.op_Inequality(approachLocation.RemoteTCPF, (ITxObject) null))
      this.SetLocationRemoteTCPF((ITxRoboticLocationOperation) locationOperation, approachLocation.RemoteTCPF);
    else if (TxFrame.op_Inequality(this.GetLocationRemoteTCPF((ITxRobot) this.m_robot, this.m_robotControllerServices, this.Operation as ITxRoboticOperation, (ITxRoboticLocationOperation) locationOperation), (ITxObject) null))
      this.SetLocationRemoteTCPF((ITxRoboticLocationOperation) locationOperation, this.GetGunTCPF(i_waypoint.KiWaypoint.BeforeWaypointActiveGun as ITxDevice));
    locationOperation.Name = i_waypoint.Name;
    this.SetApproachExternalDevicesPoses((ITxRoboticLocationOperation) locationOperation, approachLocation.AuxiliaryDevices, approachLocation.AuxiliaryDevicesPoses);
    this.SetDepartExternalDevicesPoses((ITxRoboticLocationOperation) locationOperation, approachLocation.AuxiliaryDevices, approachLocation.AuxiliaryDevicesPoses);
    this.SetLocationRobotTCPF((ITxRoboticLocationOperation) locationOperation, this.GetRobotTCPF());
    if (this.IsRedundantSystemTask())
    {
      if (approachLocation.IsTaughtPose)
        ITxRoboticLocationOperationEx.SetTaughtPose((ITxRoboticLocationOperation) locationOperation, approachLocation.RobotPose);
      else
        ITxRoboticLocationOperationEx.SetTaughtPose((ITxRoboticLocationOperation) locationOperation, (TxPoseData) null);
    }
    if (this.IsMountedWorkpieceOperationAtWaypoint(i_waypoint))
    {
      bool flag = false;
      ITxLocatableObject itxLocatableObject = (ITxLocatableObject) null;
      if (i_previousLocation is TxRoboticSeamOperation)
      {
        TxRoboticSeamOperation roboticSeamOperation = i_previousLocation as TxRoboticSeamOperation;
        if (roboticSeamOperation.Count > 0)
          itxLocatableObject = roboticSeamOperation.GetChildAt(roboticSeamOperation.Count - 1) as ITxLocatableObject;
      }
      else
      {
        itxLocatableObject = i_previousLocation as ITxLocatableObject;
        if (itxLocatableObject.AttachmentParent == null)
          itxLocatableObject = this.GetFollowingLocation(i_previousLocation) as ITxLocatableObject;
      }
      if (itxLocatableObject != null)
      {
        ITxObject attachmentParent = (ITxObject) itxLocatableObject.AttachmentParent;
        if (attachmentParent != null)
        {
          locationOperation.AttachTo(attachmentParent as ITxLocatableObject);
          flag = true;
        }
      }
      if (!flag)
      {
        TxFrame robotTcpf = this.GetRobotTCPF();
        locationOperation.AbsoluteLocation = robotTcpf.AbsoluteLocation;
        this.RemoveRemoteTCPF((ITxRoboticLocationOperation) locationOperation, this.GetRobotTCPF());
      }
    }
    TxMotionType txMotionType = this.SetMotionAndSpeedData((ITxRoboticOperation) locationOperation, i_waypoint);
    this.SetLocationZone((ITxRoboticOperation) locationOperation, i_waypoint, txMotionType);
    this.UpdateWaypointZoneLabel(i_waypoint, txMotionType);
    if (i_previousLocation == null)
      return;
    ((ITxOrderedObjectCollection) (this.Operation as ITxRoboticOrderedCompoundOperation)).MoveChildAfter((ITxObject) locationOperation, i_previousLocation);
  }

  private bool IsMountedWorkpieceOperationAtWaypoint(CApAPPWaypoint waypoint)
  {
    bool flag = false;
    return this.KiRoboticTask != null ? this.KiRoboticTask.IsMountedWorkpieceOperationAtWaypoint(waypoint.KiWaypoint) : flag;
  }

  public bool AreZonesWellDefined()
  {
    return this.LinearMotionZoneNames.Length > 1 && this.JointMotionZoneNames.Length > 1 && (this.LinearMotionZoneNames.Length == 0 || this.LinearMotionZoneNames[0].Length == 0 || !(this.LinearMotionZoneNames[0][0] == "")) && (this.JointMotionZoneNames.Length == 0 || this.JointMotionZoneNames[0].Length == 0 || !(this.JointMotionZoneNames[0][0] == ""));
  }

  protected void UpdateWaypointZoneLabel(CApAPPWaypoint i_wp, TxMotionType i_motionType)
  {
    int zoneRank = i_wp.ZoneRank;
    if (zoneRank >= this.GetMotionZoneNames(i_motionType).GetLength(0) || zoneRank == -1 || this.GetMotionZoneNames(i_motionType)[zoneRank].Length == 0)
      return;
    i_wp.ZoneLabel = this.GetMotionZoneNames(i_motionType)[zoneRank][0];
  }

  protected void SetLocationZoneParameters(ITxRoboticOperation oper, ArrayList locationZoneValues)
  {
    List<string> zoneParameters = CApAPPUtilities.GetZoneParameters(this.Robot, this.Operation);
    if ((zoneParameters == null || zoneParameters.Count == 0 || zoneParameters.Count < locationZoneValues.Count || this.Robot.Controller.Name == "default") && locationZoneValues != null && locationZoneValues.Count > 0)
    {
      TxRoboticStringParam roboticStringParam = new TxRoboticStringParam("RRS_ZONE_NAME", locationZoneValues[0] as string);
      oper.SetParameter((TxRoboticParam) roboticStringParam);
    }
    else
    {
      string globalIdString = TxEngineeringDataInternal.GetGlobalIdString((ITxObject) oper);
      TxPathEditorToDotNetGap editorToDotNetGap = new TxPathEditorToDotNetGap();
      if (editorToDotNetGap == null)
        return;
      string name = this.Robot.Controller.Name;
      for (int index = 0; index < locationZoneValues.Count; ++index)
      {
        string str = $"{name}_{zoneParameters[index]}";
        bool flag = false;
        editorToDotNetGap.HasComplexRepresentation(str, globalIdString, ref flag);
        if (flag)
        {
          string locationZoneValue = locationZoneValues[index] as string;
          editorToDotNetGap.OnComplexValueChanged(str, locationZoneValue, globalIdString);
        }
      }
    }
  }

  protected TxMotionType SetMotionAndSpeedData(
    ITxRoboticOperation roboticOperation,
    CApAPPWaypoint i_waypoint)
  {
    TxMotionType motionType = i_waypoint.TargetType == CApAPPMotionType.LinearMotion ? (TxMotionType) 2 : (TxMotionType) 1;
    int? nullable = roboticOperation.GetParameter("RRS_MOTION_TYPE") is TxRoboticIntParam parameter ? new int?(parameter.Value) : new int?();
    int num = (int) motionType;
    if (!(nullable.GetValueOrDefault() == num & nullable.HasValue))
      this.SetLocationMotionType(roboticOperation, motionType);
    this.SetLocationMotionSpeed(roboticOperation, motionType, i_waypoint.KiWaypoint.SpeedType, i_waypoint.MotionSpeed);
    this.SetLocationSpeedType(roboticOperation, i_waypoint.KiWaypoint.SpeedType);
    return motionType;
  }

  protected void SetLocationZone(
    ITxRoboticOperation oper,
    CApAPPWaypoint waypoint,
    TxMotionType motionType)
  {
    ArrayList arrayList = new ArrayList();
    if (this.KiRoboticTask != null)
    {
      int length = this.GetMotionZoneNames(motionType).Length;
      for (int index = 0; index < length; ++index)
      {
        if (this.GetMotionZoneNames(motionType)[index].Length != 0)
          arrayList.Add((object) new ArrayList()
          {
            (object) this.GetMotionZoneNames(motionType)[index][0]
          });
      }
    }
    int zoneRank = waypoint.ZoneRank;
    if (zoneRank == -1)
    {
      ArrayList locationZoneValues = (ArrayList) null;
      for (int index = 0; index < arrayList.Count; ++index)
      {
        if (waypoint.ZoneLabel == ((ArrayList) arrayList[index])[0] as string)
        {
          locationZoneValues = (ArrayList) arrayList[index];
          break;
        }
      }
      if (locationZoneValues == null)
      {
        if (waypoint.ZoneLabel == "fine")
        {
          locationZoneValues = this.GetFineZone(motionType);
        }
        else
        {
          locationZoneValues = new ArrayList();
          locationZoneValues.Add((object) waypoint.ZoneLabel);
        }
      }
      this.SetLocationZoneParameters(oper, locationZoneValues);
    }
    else
    {
      if (zoneRank >= arrayList.Count)
        return;
      ArrayList locationZoneValues = arrayList[zoneRank] as ArrayList;
      this.SetLocationZoneParameters(oper, locationZoneValues);
    }
  }

  private int GetRankOfJoint(ITxDevice i_dev, TxJoint i_jnt)
  {
    int rankOfJoint = -1;
    for (int index = 0; index < ((Collection<ITxObject>) i_dev.DrivingJoints).Count; ++index)
    {
      if (((Collection<ITxObject>) i_dev.DrivingJoints)[index] == i_jnt)
      {
        rankOfJoint = index;
        break;
      }
    }
    return rankOfJoint;
  }

  private void SetApproachExternalDevicesPoses(
    ITxRoboticLocationOperation operation,
    TxObjectList<ITxDevice> currentAuxDevices,
    List<TxPoseData> currentAuxDevicesPoses)
  {
    TxRobotExternalAxisData[] externalAxisDataArray = this.GeExternalAxisData(operation, currentAuxDevices, currentAuxDevicesPoses);
    if (externalAxisDataArray == null)
      return;
    operation.RobotExternalAxesData = externalAxisDataArray;
  }

  private void SetDepartExternalDevicesPoses(
    ITxRoboticLocationOperation operation,
    TxObjectList<ITxDevice> currentAuxDevices,
    List<TxPoseData> currentAuxDevicesPoses)
  {
    TxRobotExternalAxisData[] externalAxisDataArray = this.GeExternalAxisData(operation, currentAuxDevices, currentAuxDevicesPoses);
    if (externalAxisDataArray == null)
      return;
    operation.RobotDepartureExternalAxesData = externalAxisDataArray;
  }

  private TxRobotExternalAxisData[] GeExternalAxisData(
    ITxRoboticLocationOperation operation,
    TxObjectList<ITxDevice> currentAuxDevices,
    List<TxPoseData> currentAuxDevicesPoses)
  {
    TxRobot robot = this.Robot as TxRobot;
    TxRobotExternalAxisData[] externalAxisDataArray = (TxRobotExternalAxisData[]) null;
    if (TxRobot.op_Inequality(robot, (ITxObject) null) && currentAuxDevicesPoses != null)
    {
      TxObjectList externalAxes = robot.ExternalAxes;
      ArrayList arrayList = new ArrayList();
      for (int index1 = 0; index1 < ((Collection<ITxObject>) externalAxes).Count; ++index1)
      {
        TxJoint i_jnt = ((Collection<ITxObject>) externalAxes)[index1] as TxJoint;
        int rankOfJoint = this.GetRankOfJoint(i_jnt.Device, i_jnt);
        double num = 0.0;
        for (int index2 = 0; index2 < ((Collection<ITxDevice>) currentAuxDevices).Count; ++index2)
        {
          if (((Collection<ITxDevice>) currentAuxDevices)[index2] == i_jnt.Device)
          {
            num = (double) currentAuxDevicesPoses[index2].JointValues[rankOfJoint];
            break;
          }
        }
        arrayList.Add((object) new TxRobotExternalAxisData()
        {
          Device = i_jnt.Device,
          Joint = i_jnt,
          JointValue = num
        });
      }
      externalAxisDataArray = new TxRobotExternalAxisData[arrayList.Count];
      for (int index = 0; index < arrayList.Count; ++index)
        externalAxisDataArray[index] = arrayList[index] as TxRobotExternalAxisData;
    }
    return externalAxisDataArray;
  }

  private void SetLocationMotionType(ITxRoboticOperation loc, TxMotionType motionType)
  {
    TxRoboticIntParam txRoboticIntParam = new TxRoboticIntParam("RRS_MOTION_TYPE", (int) motionType);
    loc.SetParameter((TxRoboticParam) txRoboticIntParam);
    new TxPathEditorToDotNetGap().OnValueChanged("_RRS_MOTION_TYPE", TxEngineeringDataInternal.GetGlobalIdString((ITxObject) loc));
  }

  private void SetLocationMotionSpeed(
    ITxRoboticOperation loc,
    TxMotionType motionType,
    KiAppSpeedType speedType,
    double motionSpeed)
  {
    if (speedType == null)
    {
      string speedParameterName = CApAPPRoboticTask.GetMotionSpeedParameterName(motionType, speedType);
      if (!(speedParameterName != "") || motionSpeed < 0.0)
        return;
      TxRoboticDoubleParam roboticDoubleParam = new TxRoboticDoubleParam(speedParameterName, motionSpeed);
      loc.SetParameter((TxRoboticParam) roboticDoubleParam);
    }
    else
    {
      string speedParameterName = CApAPPRoboticTask.GetMotionSpeedParameterName(motionType, speedType);
      if (!(speedParameterName != "") || motionSpeed < 0.0)
        return;
      TxRoboticIntParam txRoboticIntParam = new TxRoboticIntParam(speedParameterName, (int) motionSpeed);
      loc.SetParameter((TxRoboticParam) txRoboticIntParam);
    }
  }

  private void SetLocationSpeedType(ITxRoboticOperation loc, KiAppSpeedType speedType)
  {
    string typeParameterName = CApAPPRoboticTask.GetSpeedTypeParameterName(loc);
    if (!(typeParameterName != ""))
      return;
    string str = speedType == 1 ? "Joint Speed" : "Cartesian Speed";
    TxRoboticStringParam roboticStringParam = new TxRoboticStringParam(typeParameterName, str);
    loc.SetParameter((TxRoboticParam) roboticStringParam);
  }

  public ArrayList GetLocationZoneParameters(ITxRoboticOperation loc)
  {
    ArrayList locationZoneParameters = new ArrayList();
    if (this.Robot.Controller.Name == "default")
    {
      if (loc.GetParameter("RRS_ZONE_NAME") is TxRoboticStringParam parameter)
      {
        string str = parameter.Value;
        if (str != null && str != "")
          locationZoneParameters.Add((object) str);
      }
    }
    else
    {
      string name = this.Robot.Controller.Name;
      List<string> zoneParameters = CApAPPUtilities.GetZoneParameters(this.Robot, this.Operation);
      TxPathEditorToDotNetGap editorToDotNetGap = new TxPathEditorToDotNetGap();
      string globalIdString = TxEngineeringDataInternal.GetGlobalIdString((ITxObject) loc);
      for (int index = 0; index < zoneParameters.Count; ++index)
      {
        string str1 = zoneParameters[index];
        string str2 = $"{name}_{str1}";
        string str3 = "";
        bool flag = false;
        editorToDotNetGap.HasComplexRepresentation(str2, globalIdString, ref flag);
        if (flag)
        {
          editorToDotNetGap.GetComplexRepresentation(ref str3, globalIdString, str2);
          if (str3 != null)
            str3 = str3.Trim();
        }
        locationZoneParameters.Add((object) str3);
      }
    }
    return locationZoneParameters;
  }

  private string GetLocationZoneLabel(ITxRoboticLocationOperation loc)
  {
    string locationZoneLabel = "";
    if (loc != null)
    {
      ArrayList locationZoneParameters = this.GetLocationZoneParameters((ITxRoboticOperation) loc);
      if (locationZoneParameters.Count > 0)
        locationZoneLabel = locationZoneParameters[0] as string;
    }
    return locationZoneLabel;
  }

  internal static TxMotionType GetLocationMotionType(ITxRoboticLocationOperation loc)
  {
    TxMotionType locationMotionType = (TxMotionType) 1;
    TxRoboticIntParam parameter = (TxRoboticIntParam) ((ITxRoboticOperation) loc).GetParameter("RRS_MOTION_TYPE");
    if (parameter != null)
      locationMotionType = (TxMotionType) parameter.Value;
    return locationMotionType;
  }

  internal static KiAppSpeedType GetLocationSpeedType(
    ITxRobot robot,
    ITxRoboticLocationOperation loc)
  {
    return (TxRoboticIntParam) ((ITxRoboticOperation) loc).GetParameter("KAWA_SPEED_NUM") == null ? (!robot.Controller.Name.Contains("Kawasaki") ? (KiAppSpeedType) 0 : (KiAppSpeedType) 1) : (KiAppSpeedType) 1;
  }

  internal static double GetLocationMotionSpeed(
    ITxRoboticOperation loc,
    TxMotionType motionType,
    KiAppSpeedType speedType)
  {
    double locationMotionSpeed = -1.0;
    string speedParameterName = CApAPPRoboticTask.GetMotionSpeedParameterName(motionType, speedType);
    TxRoboticParam parameter1 = loc.GetParameter(speedParameterName);
    if (parameter1 != null && parameter1 is TxRoboticDoubleParam)
    {
      if (loc.GetParameter(speedParameterName) is TxRoboticDoubleParam parameter2)
        locationMotionSpeed = parameter2.Value;
    }
    else if (parameter1 != null && parameter1 is TxRoboticIntParam && loc.GetParameter(speedParameterName) is TxRoboticIntParam parameter3)
      locationMotionSpeed = (double) parameter3.Value;
    return locationMotionSpeed;
  }

  private static string GetMotionSpeedParameterName(
    TxMotionType motionType,
    KiAppSpeedType speedType)
  {
    string speedParameterName = "";
    if (speedType == null)
    {
      if (motionType == 1)
        speedParameterName = "RRS_JOINT_SPEED";
      else if (motionType == 2)
        speedParameterName = "RRS_CARTESIAN_POSITION_SPEED";
    }
    else
      speedParameterName = "KAWA_SPEED_NUM";
    return speedParameterName;
  }

  private static string GetSpeedTypeParameterName(ITxRoboticOperation loc)
  {
    return (TxRoboticStringParam) loc.GetParameter("TP_SPEED_TYPE") != null ? "TP_SPEED_TYPE" : "";
  }

  private void SetLocationRobotTCPF(ITxRoboticLocationOperation loc, TxFrame frame)
  {
    if (!TxFrame.op_Inequality(frame, (ITxObject) null) || loc == null)
      return;
    TxRoboticTxObjectParam roboticTxObjectParam = new TxRoboticTxObjectParam("RRS_TOOL_FRAME", (ITxObject) frame);
    ((ITxRoboticOperation) loc).SetParameter((TxRoboticParam) roboticTxObjectParam);
  }

  private TxFrame GetLocationRobotTCPF(ITxRoboticLocationOperation loc)
  {
    TxFrame locationRobotTcpf = (TxFrame) null;
    if (loc != null)
    {
      TxRoboticTxObjectParam parameter = (TxRoboticTxObjectParam) ((ITxRoboticOperation) loc).GetParameter("RRS_TOOL_FRAME");
      if (parameter != null)
        locationRobotTcpf = parameter.Value as TxFrame;
    }
    return locationRobotTcpf;
  }

  private TxFrame GetLocationRemoteTCPF(
    ITxRobot robot,
    ITxRoboticControllerServices controllerServices,
    ITxRoboticOperation operation,
    ITxRoboticLocationOperation loc)
  {
    TxFrame locationRemoteTcpf = (TxFrame) null;
    if (loc != null)
    {
      TxRoboticTxObjectParam parameter = (TxRoboticTxObjectParam) ((ITxRoboticOperation) loc).GetParameter("MOUNTED_WORKPIECE_FRAME_NAME");
      locationRemoteTcpf = parameter == null ? this.GetExternalGunTCPF(robot, controllerServices, operation, loc) : parameter.Value as TxFrame;
    }
    return locationRemoteTcpf;
  }

  private void SetLocationRemoteTCPF(ITxRoboticLocationOperation loc, TxFrame frame)
  {
    if (!TxFrame.op_Inequality(frame, (ITxObject) null) || loc == null)
      return;
    TxRoboticTxObjectParam roboticTxObjectParam = new TxRoboticTxObjectParam("MOUNTED_WORKPIECE_FRAME_NAME", (ITxObject) frame);
    ((ITxRoboticOperation) loc).SetParameter((TxRoboticParam) roboticTxObjectParam);
  }

  private void RemoveRemoteTCPF(ITxRoboticLocationOperation loc, TxFrame frame)
  {
    if (!TxFrame.op_Inequality(frame, (ITxObject) null) || loc == null)
      return;
    ((ITxRoboticOperation) loc).RemoveParameter("MOUNTED_WORKPIECE_FRAME_NAME");
  }

  public void GetExternalAxisJointNames(
    ref List<string> o_externalAxisJointNames,
    ref List<string> o_devicesJointNames)
  {
    o_externalAxisJointNames.Clear();
    o_devicesJointNames.Clear();
    TxRobot robot = this.Robot as TxRobot;
    if (!TxRobot.op_Inequality(robot, (ITxObject) null))
      return;
    for (int index = 0; index < ((Collection<ITxObject>) robot.ExternalAxes).Count; ++index)
    {
      TxJoint externalAx = ((Collection<ITxObject>) this.m_robot.ExternalAxes)[index] as TxJoint;
      if (TxJoint.op_Inequality(externalAx, (ITxObject) null))
      {
        o_externalAxisJointNames.Add(externalAx.Name);
        o_devicesJointNames.Add(((ITxObject) externalAx.Device).Name);
      }
    }
  }

  public List<string> GetMainDeviceJointNames()
  {
    List<string> deviceJointNames = new List<string>();
    TxRobot robot = this.Robot as TxRobot;
    if (TxRobot.op_Inequality(robot, (ITxObject) null))
    {
      TxObjectList drivingJoints = robot.DrivingJoints;
      for (int index = 0; index < ((Collection<ITxObject>) drivingJoints).Count; ++index)
        deviceJointNames.Add(((Collection<ITxObject>) drivingJoints)[index].Name);
    }
    return deviceJointNames;
  }

  private void SetExternalAxesJoints(
    TxRoboticViaLocationOperation locOp,
    ITxDevice device,
    TxPoseData txPoseData)
  {
    TxRobot robot = this.Robot as TxRobot;
    if (!TxRobot.op_Inequality(robot, (ITxObject) null) || device == null)
      return;
    TxObjectList drivingJoints = device.DrivingJoints;
    TxObjectList externalAxes = robot.ExternalAxes;
    ArrayList arrayList = new ArrayList();
    for (int index = 0; index < ((Collection<ITxObject>) drivingJoints).Count; ++index)
    {
      TxJoint txJoint = ((Collection<ITxObject>) drivingJoints)[index] as TxJoint;
      if (((Collection<ITxObject>) externalAxes).Contains((ITxObject) txJoint))
        arrayList.Add((object) new TxRobotExternalAxisData()
        {
          Device = device,
          Joint = txJoint,
          JointValue = (double) txPoseData.JointValues[index]
        });
    }
    TxRobotExternalAxisData[] externalAxisDataArray = new TxRobotExternalAxisData[arrayList.Count];
    for (int index = 0; index < arrayList.Count; ++index)
      externalAxisDataArray[index] = arrayList[index] as TxRobotExternalAxisData;
    locOp.RobotExternalAxesData = externalAxisDataArray;
  }

  private void FillLocationParameter(
    TxRoboticViaLocationOperation i_operation,
    ITxRoboticLocationOperation i_locToCopy)
  {
    if (i_locToCopy == null)
      return;
    foreach (TxRoboticParam parameter in ((ITxRoboticOperation) i_locToCopy).Parameters)
    {
      if (parameter.Type != "SW_GUN_WAIT" && parameter.Type != "SW_GUN_STATE" && parameter.Type != "POSITION_IN_SIMULATION" && parameter.Type != "OLP_POSE_TARGET" && parameter.Type != "OLP_TAUGHT_POSE_DATA" && parameter.Type != "ProcessType" && !parameter.Type.Contains("KAWASAKI_CLAMP"))
        i_operation.SetParameter(parameter);
      else if (parameter.Type.Contains("KAWASAKI_CLAMP"))
      {
        if (parameter is TxRoboticStringParam roboticStringParam && roboticStringParam.Value.Contains("ON"))
        {
          string str = roboticStringParam.Value.Replace("ON", "OFF");
          i_operation.SetParameter((TxRoboticParam) new TxRoboticStringParam(roboticStringParam)
          {
            Value = str
          });
        }
        else
          i_operation.SetParameter(parameter);
      }
    }
  }

  private void UpdateLocationParametersFromWaypoint(
    TxRoboticViaLocationOperation i_operation,
    CApAPPWaypoint i_waypoint)
  {
    foreach (TxRoboticParam parameter in i_operation.Parameters)
    {
      if (parameter.Type == "waypointPreprocessSuccessStatus")
      {
        TxRoboticStringParam roboticStringParam = new TxRoboticStringParam();
        ((TxRoboticParam) roboticStringParam).Type = parameter.Type;
        roboticStringParam.Value = i_waypoint.WaypointPlanSuccess == CApAPPWaypointPlanSuccess.PreProcessingSuccess ? "PreProcessingSuccess" : "NotPreProcessingSuccess";
        i_operation.SetParameter((TxRoboticParam) roboticStringParam);
      }
      else if (parameter.Type == "waypointFixedStatus")
      {
        TxRoboticStringParam roboticStringParam = new TxRoboticStringParam();
        ((TxRoboticParam) roboticStringParam).Type = parameter.Type;
        roboticStringParam.Value = i_waypoint.Fixed ? "Fixed" : "NotFixed";
        i_operation.SetParameter((TxRoboticParam) roboticStringParam);
      }
    }
  }

  private TxRobotConfigurationData GetCorrectConfigurationFromPose(
    TxPoseData poseData,
    TxTransformation trans)
  {
    ArrayList arrayList = this.Robot.CalcInverseSolutions(new TxRobotInverseData(trans, (TxRobotInverseData.TxInverseType) 0));
    TxPoseData txPoseData = poseData;
    for (int index = 0; index < arrayList.Count; ++index)
    {
      if (CApAPPUtilities.PosesAreEquivalent(poseData, arrayList[index] as TxPoseData))
      {
        txPoseData = arrayList[index] as TxPoseData;
        break;
      }
    }
    return this.Robot.GetPoseConfiguration(txPoseData);
  }

  public TxRoboticViaLocationOperation CreateViaLocationOperation(
    KiAppRoboticLocationEx robotLocation)
  {
    bool flag = TxApplicationEx.IsInEnabledMode(new List<TxApplicationModeEx>()
    {
      new TxApplicationModeEx("SIMULATION_WITHOUT_REFRESH_MODE", false)
    });
    if (flag)
      TxApplicationEx.EnterMode(new TxApplicationModeEx("SIMULATION_WITHOUT_REFRESH_MODE", true));
    TxRoboticViaLocationOperation locationOperation = (TxRoboticViaLocationOperation) null;
    (this.Robot as TxRobot).CurrentPose = robotLocation.RobotPose;
    TxTransformation absoluteLocation = ((KiAppLocationEx) robotLocation).AbsoluteLocation;
    using (TxRoboticViaLocationOperationCreationData operationCreationData = new TxRoboticViaLocationOperationCreationData())
    {
      operationCreationData.AbsoluteLocation = absoluteLocation;
      if (this.Operation is ITxWeldOperation)
        locationOperation = ((ITxRoboticViaLocationOperationCreation) (this.Operation as ITxWeldOperation)).CreateRoboticViaLocationOperation(operationCreationData);
      else if (this.Operation is ITxContinuousOperation)
        locationOperation = ((ITxRoboticViaLocationOperationCreation) (this.Operation as ITxContinuousOperation)).CreateRoboticViaLocationOperation(operationCreationData);
      else if (this.Operation is TxGenericRoboticOperation)
        locationOperation = ((TxBaseGenericRoboticOperation) (this.Operation as TxGenericRoboticOperation)).CreateRoboticViaLocationOperation(operationCreationData);
    }
    locationOperation.RobotConfigurationData = !robotLocation.IsTaughtConfiguration || !CApAPPRoboticTask.IsConfigDataValid(this.Robot.GetPoseConfiguration(robotLocation.RobotPose)) ? (TxRobotConfigurationData) null : this.Robot.GetPoseConfiguration(robotLocation.RobotPose);
    if (flag)
      TxApplicationEx.ExitMode(new TxApplicationModeEx("SIMULATION_WITHOUT_REFRESH_MODE", true));
    return locationOperation;
  }

  public override TxObjectList GetAllLocations()
  {
    TxObjectList allLocations = new TxObjectList();
    for (int index = 0; index < ((ITxObjectCollection) (this.Operation as ITxOrderedObjectCollection)).Count; ++index)
    {
      ITxObject childAt = (this.Operation as ITxOrderedObjectCollection).GetChildAt(index);
      if (childAt != null && (!(childAt is TxWeldLocationOperation) || (childAt as TxWeldLocationOperation).IsProjected))
        ((Collection<ITxObject>) allLocations).Add(childAt);
    }
    return allLocations;
  }

  private TxFrame GetExternalGunTCPF(
    ITxRobot robot,
    ITxRoboticControllerServices roboticControllerServices,
    ITxRoboticOperation operation,
    ITxRoboticLocationOperation loc)
  {
    ITxDevice gun = (ITxDevice) null;
    if (operation.IsMountedWorkpieceOperation && operation is ITxWeldOperation)
      gun = (this.Operation as ITxWeldOperation).Gun as ITxDevice;
    else if (operation.IsMountedWorkpieceOperation && operation is ITxContinuousOperation)
      gun = (this.Operation as ITxContinuousOperation).Tool as ITxDevice;
    else if (this.GetActiveMountedGun(robot) == null)
    {
      ITxGun rcsGun = this.GetRCSGun(robot, roboticControllerServices, loc);
      if (!this.IsComponentAttachedToDevice((ITxDevice) (robot as TxRobot), rcsGun as ITxComponent))
        gun = rcsGun as ITxDevice;
    }
    return this.GetGunTCPF(gun);
  }

  private bool IsComponentAttachedToObject(ITxObject myObject, ITxComponent component)
  {
    bool flag = false;
    if (myObject == component)
    {
      flag = true;
    }
    else
    {
      TxTypeFilter txTypeFilter = new TxTypeFilter();
      txTypeFilter.AddIncludedType(typeof (ITxObject));
      if (myObject is ITxLocatableObject)
      {
        foreach (ITxObject attachmentDescendant in (Collection<ITxObject>) (myObject as ITxLocatableObject).GetDirectAttachmentDescendants((ITxTypeFilter) txTypeFilter))
        {
          flag = this.IsComponentAttachedToObject(attachmentDescendant, component);
          if (flag)
            break;
        }
      }
      if (!flag && myObject is ITxObjectCollection)
      {
        foreach (ITxObject directDescendant in (Collection<ITxObject>) (myObject as ITxObjectCollection).GetDirectDescendants((ITxTypeFilter) txTypeFilter))
        {
          flag = this.IsComponentAttachedToObject(directDescendant, component);
          if (flag)
            break;
        }
      }
    }
    return flag;
  }

  private bool IsComponentAttachedToDevice(ITxDevice device, ITxComponent component)
  {
    bool device1 = false;
    TxObjectList links = device.Links;
    new TxTypeFilter().AddIncludedType(typeof (ITxObject));
    if (((Collection<ITxObject>) links).Count > 0)
      device1 = this.IsComponentAttachedToObject(((Collection<ITxObject>) links)[0], component);
    return device1;
  }

  private TxFrame GetGunTCPF(ITxDevice gun)
  {
    TxFrame gunTcpf = (TxFrame) null;
    switch (gun)
    {
      case TxGun _:
        gunTcpf = (gun as TxGun).TCPF;
        break;
      case TxServoGun _:
        gunTcpf = (gun as TxServoGun).TCPF;
        break;
      case TxGripper _:
        gunTcpf = (gun as TxGripper).TCPF;
        break;
    }
    return gunTcpf;
  }

  public TxFrame GetRobotTCPF()
  {
    TxFrame robotTcpf = (TxFrame) null;
    ITxRoboticLocationOperation lastLocation1 = this.GetLastLocation() as ITxRoboticLocationOperation;
    TxRoboticSeamOperation lastLocation2 = this.GetLastLocation() as TxRoboticSeamOperation;
    ITxRoboticLocationOperation locationOperation = !TxRoboticSeamOperation.op_Inequality(lastLocation2, (ITxObject) null) || lastLocation2.Count <= 0 ? lastLocation1 : lastLocation2.GetChildAt(0) as ITxRoboticLocationOperation;
    if (locationOperation != null)
    {
      TxRoboticTxObjectParam parameter = (TxRoboticTxObjectParam) ((ITxRoboticOperation) locationOperation).GetParameter("RRS_TOOL_FRAME");
      if (parameter != null)
        robotTcpf = parameter.Value as TxFrame;
    }
    return robotTcpf;
  }

  public override CApAPPWaypoint Waypoint(int i_index)
  {
    CApAPPWaypoint i_wp = new CApAPPWaypoint((KiAppWaypointEx) (this.KiTask.GetWaypoint(i_index) as KiAppRoboticWaypointEx));
    this.configureWaypoint(i_wp, i_index);
    return i_wp;
  }

  public override void Waypoint(int i_index, CApAPPWaypoint i_wp)
  {
    this.KiTask.SetWaypoint(i_index, i_wp.KiWaypoint);
  }

  public ITxRobot Robot => (ITxRobot) this.m_robot;

  protected KiAppRoboticTaskEx KiRoboticTask => this.KiTask as KiAppRoboticTaskEx;

  public string[][] GetMotionZoneNames(TxMotionType i_type)
  {
    return i_type == 1 ? this.JointMotionZoneNames : this.LinearMotionZoneNames;
  }

  public void SetMotionZoneNames(TxMotionType i_type, string[][] i_motionZoneNames)
  {
    if (i_type == 1)
      this.JointMotionZoneNames = i_motionZoneNames;
    else
      this.LinearMotionZoneNames = i_motionZoneNames;
  }

  public string[] ZoneParameters
  {
    get => this.KiRoboticTask.ZoneParameters;
    set => this.KiRoboticTask.ZoneParameters = value;
  }

  public string[][] LinearMotionZoneNames
  {
    get => this.KiRoboticTask.LinearMotionZoneNames;
    set => this.KiRoboticTask.LinearMotionZoneNames = value;
  }

  public string[][] JointMotionZoneNames
  {
    get => this.KiRoboticTask.JointMotionZoneNames;
    set => this.KiRoboticTask.JointMotionZoneNames = value;
  }

  public double[] DrivingJointsWeight
  {
    get => this.KiRoboticTask.DrivingJointsWeight;
    set => this.KiRoboticTask.DrivingJointsWeight = value;
  }

  public double[] GunAndAuxiliaryDrivingJointsWeight
  {
    get => this.KiRoboticTask.GunAndAuxiliaryDrivingJointsWeight;
    set => this.KiRoboticTask.GunAndAuxiliaryDrivingJointsWeight = value;
  }

  public void SetDefaultDrivingJointsWeights()
  {
    this.KiRoboticTask.SetDefaultDrivingJointsWeights();
  }

  public void SetDefaultZoneNames()
  {
    this.KiRoboticTask.SetDefaultZoneNames();
    this.EnsureDefaultZoneIsInSetZoneNames();
  }

  private void EnsureDefaultZoneIsInSetZoneNames()
  {
    List<string> zoneParameters = CApAPPUtilities.GetZoneParameters(this.Robot, this.Operation);
    List<List<string>> allRobotsZone1 = CApAPPUtilities.GetAllRobotsZone(this.Robot, this.Operation, zoneParameters, (TxMotionType) 2);
    string[][] linearMotionZoneNames = this.LinearMotionZoneNames;
    CApAPPRoboticTask.EnsureDefaultZoneIsInZoneList(allRobotsZone1, ref linearMotionZoneNames);
    this.LinearMotionZoneNames = linearMotionZoneNames;
    List<List<string>> allRobotsZone2 = CApAPPUtilities.GetAllRobotsZone(this.Robot, this.Operation, zoneParameters, (TxMotionType) 1);
    string[][] jointMotionZoneNames = this.JointMotionZoneNames;
    CApAPPRoboticTask.EnsureDefaultZoneIsInZoneList(allRobotsZone2, ref jointMotionZoneNames);
    this.JointMotionZoneNames = jointMotionZoneNames;
  }

  public static void EnsureDefaultZoneIsInZoneList(
    List<List<string>> allRobotZonesByParameter,
    ref string[][] zones)
  {
    bool flag1 = false;
    foreach (string[] source in zones)
    {
      bool flag2 = true;
      for (int index = 0; index < ((IEnumerable<string>) source).Count<string>(); ++index)
      {
        if (source[index] != allRobotZonesByParameter[index][0])
          flag2 = false;
      }
      if (flag2)
      {
        flag1 = true;
        break;
      }
    }
    if (flag1)
      return;
    int length = 0;
    for (int index = 0; index < allRobotZonesByParameter.Count && allRobotZonesByParameter[index].Count != 0 && (allRobotZonesByParameter[index].Count != 1 || !(allRobotZonesByParameter[index][0] == "")); ++index)
      ++length;
    string[][] strArray = new string[zones.Length + 1][];
    strArray[0] = new string[length];
    for (int index = 0; index < length; ++index)
      strArray[0][index] = allRobotZonesByParameter[index][0];
    for (int index1 = 1; index1 < strArray.Length; ++index1)
    {
      strArray[index1] = new string[length];
      for (int index2 = 0; index2 < length; ++index2)
        strArray[index1][index2] = zones[index1 - 1][index2];
    }
    zones = strArray;
  }

  public void EnableCableTwistingCheck(double i_maxAbsJ4J6)
  {
    this.KiRoboticTask.EnableCableTwistingCheck(i_maxAbsJ4J6);
  }

  public void DisableCableTwistingCheck() => this.KiRoboticTask.DisableCableTwistingCheck();

  private bool AreAllOLPCommandsSupported(TxObjectList olpCommands)
  {
    bool flag = true;
    for (int index = 0; index < ((Collection<ITxObject>) olpCommands).Count & flag; ++index)
    {
      string text = (((Collection<ITxObject>) olpCommands)[index] as TxRoboticCommand).Text;
      flag = false;
      if (text.Contains("Destination") || text.Contains("Drive") || text.Contains("WaitDevice") || text.Contains("Grip") || text.Contains("Release") || text.Contains("Mount") || text.Contains("UnMount") || text.Contains("Attach") || text.Contains("Detach"))
        flag = true;
    }
    return flag;
  }

  public bool doesContainsLogicOLPCommands()
  {
    bool flag = false;
    string[] source = new string[15]
    {
      "IF",
      "ELSIF",
      "ELSE",
      "ENDIF",
      "SWITCH",
      "CASE",
      "DEFAULT",
      "ENDSWITCH",
      "WHILE",
      "ENDWHILE",
      "FOR",
      "ENDFOR",
      "GOTO",
      "JUMP",
      "CALL"
    };
    TxObjectList allLocations = this.GetAllLocations();
    for (int index1 = 0; index1 < ((Collection<ITxObject>) allLocations).Count && !flag; ++index1)
    {
      if (((Collection<ITxObject>) allLocations)[index1] is ITxRoboticOperation)
      {
        TxObjectList commands = (((Collection<ITxObject>) allLocations)[index1] as ITxRoboticOperation).Commands;
        for (int index2 = 0; index2 < ((Collection<ITxObject>) commands).Count && !flag; ++index2)
        {
          if (((Collection<ITxObject>) commands)[index2] is TxRoboticCommand)
          {
            string upper = (((Collection<ITxObject>) commands)[index2] as TxRoboticCommand).Text.ToUpper();
            for (int index3 = 0; index3 < ((IEnumerable<string>) source).Count<string>() && !flag; ++index3)
            {
              if (upper.Contains(source[index3]))
                flag = true;
            }
          }
        }
      }
    }
    return flag;
  }

  private void ClearOperationOLPCommands()
  {
    if (!(this.Operation is ITxRoboticOrderedCompoundOperation operation) || ((ITxRoboticOperation) operation).Commands == null)
      return;
    for (int index = ((Collection<ITxObject>) ((ITxRoboticOperation) operation).Commands).Count - 1; index > -1; --index)
      (((Collection<ITxObject>) ((ITxRoboticOperation) operation).Commands)[index] as TxRoboticCommand).Delete();
  }

  private void AddDriveDeviceOLPCommand(CApAPPWaypoint i_waypoint)
  {
    if (!(this.Operation is ITxRoboticOrderedCompoundOperation operation))
      return;
    TxRoboticCommandCreationData dataFromLocation = this.CreateCommandCreationDataFromLocation(i_waypoint.SimulatedApproachLocation as KiAppRoboticLocationEx);
    ((ITxRoboticOperation) operation).CreateCommand(dataFromLocation);
  }

  private TxRoboticCommandCreationData CreateCommandCreationDataFromLocation(
    KiAppRoboticLocationEx i_location)
  {
    string str1 = "# DriveDeviceToValuesSynch " + ((ITxObject) this.Robot).Name;
    for (int index = 0; index < i_location.RobotPose.JointValues.Count; ++index)
    {
      string str2 = $"{str1} {((Collection<ITxObject>) (this.Robot as TxRobot).DrivingJoints)[index].Name}:";
      str1 = (((Collection<ITxObject>) (this.Robot as TxRobot).DrivingJoints)[index] as TxJoint).Type != 1 ? str2 + i_location.RobotPose.JointValues[index]?.ToString() : str2 + ((double) i_location.RobotPose.JointValues[index] * (180.0 / Math.PI)).ToString();
    }
    return new TxRoboticCommandCreationData(str1);
  }

  private List<ArrayList> GetInitialParameters(ITxRoboticOperation i_roboticOperation)
  {
    List<ArrayList> initialParameters = new List<ArrayList>();
    TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations((ITxOperation) i_roboticOperation);
    for (int index = 0; index < ((Collection<ITxObject>) operationLocations).Count; ++index)
      initialParameters.Add((((Collection<ITxObject>) operationLocations)[index] as ITxRoboticOperation).Parameters);
    return initialParameters;
  }

  private void SetOperationParameters(
    ITxRoboticOperation i_roboticOperation,
    List<ArrayList> parameters)
  {
    TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations((ITxOperation) i_roboticOperation);
    for (int index = 0; index < ((Collection<ITxObject>) operationLocations).Count; ++index)
    {
      if (index < parameters.Count)
      {
        foreach (TxRoboticParam parameter in (((Collection<ITxObject>) operationLocations)[index] as ITxRoboticOperation).Parameters)
          (((Collection<ITxObject>) operationLocations)[index] as ITxRoboticOperation).RemoveParameter(parameter.Type);
        foreach (TxRoboticParam txRoboticParam in parameters[index])
          (((Collection<ITxObject>) operationLocations)[index] as ITxRoboticOperation).SetParameter(txRoboticParam);
      }
    }
  }

  public ArrayList GetInitialZones(ITxRoboticOperation i_roboticOperation)
  {
    ArrayList initialZones = new ArrayList();
    TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations((ITxOperation) i_roboticOperation);
    for (int index = 0; index < ((Collection<ITxObject>) operationLocations).Count; ++index)
      initialZones.Add((object) this.GetLocationZoneParameters(((Collection<ITxObject>) operationLocations)[index] as ITxRoboticOperation));
    return initialZones;
  }

  private void SetOperationZones(ITxRoboticOperation i_roboticOperation, ArrayList zones)
  {
    TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations((ITxOperation) i_roboticOperation);
    for (int index = 0; index < ((Collection<ITxObject>) operationLocations).Count; ++index)
    {
      if (index < zones.Count && ((Collection<ITxObject>) operationLocations)[index] is ITxRoboticLocationOperation)
        this.SetLocationZoneParameters((ITxRoboticOperation) (((Collection<ITxObject>) operationLocations)[index] as ITxRoboticLocationOperation), zones[index] as ArrayList);
    }
  }

  private ArrayList GetFineZone(TxMotionType motionType)
  {
    List<List<string>> allRobotsZone = CApAPPUtilities.GetAllRobotsZone(this.Robot, this.Operation, CApAPPUtilities.GetZoneParameters(this.Robot, this.Operation), motionType);
    ArrayList fineZone = new ArrayList();
    if (allRobotsZone.Count > 0)
    {
      for (int index = 0; index < allRobotsZone.Count; ++index)
      {
        if (allRobotsZone[index].Count > 0 && allRobotsZone[index][0] != "")
          fineZone.Add((object) allRobotsZone[index][0]);
      }
    }
    return fineZone;
  }

  private void ResetAllZonesToFine(ITxRoboticOperation i_roboticOperation)
  {
    ArrayList fineZone1 = this.GetFineZone((TxMotionType) 2);
    ArrayList fineZone2 = this.GetFineZone((TxMotionType) 1);
    TxObjectList operationLocations = CApAPPUtilities.GetOperationLocations((ITxOperation) i_roboticOperation);
    for (int index = 0; index < ((Collection<ITxObject>) operationLocations).Count; ++index)
    {
      ITxObject itxObject = ((Collection<ITxObject>) operationLocations)[index];
      if (itxObject is ITxRoboticLocationOperation)
      {
        TxMotionType locationMotionType = CApAPPRoboticTask.GetLocationMotionType(itxObject as ITxRoboticLocationOperation);
        this.SetLocationZoneParameters(itxObject as ITxRoboticOperation, locationMotionType == 2 ? fineZone1 : fineZone2);
      }
    }
  }

  internal static bool IsConfigDataValid(TxRobotConfigurationData configData)
  {
    bool flag = false;
    if (configData != null)
    {
      if (configData.OverheadState != 1)
        flag = true;
      foreach (TxJointConfigurationData jointsConfiguration in configData.JointsConfigurations)
      {
        if (jointsConfiguration.State != 1)
          flag = true;
        if (jointsConfiguration.Turn.HasValue)
          flag = true;
      }
    }
    return flag;
  }

  private void AddHandledDevices(TxObjectList<ITxDevice> handledDevices)
  {
    foreach (ITxDevice handledDevice in (Collection<ITxDevice>) handledDevices)
    {
      bool flag = false;
      for (int index = 0; index < this.m_defaultDescriptor.DeviceJointValuesPairsCount; ++index)
      {
        if (this.m_defaultDescriptor.GetDeviceJointValuesPairAt(index).Key == handledDevice)
        {
          flag = true;
          break;
        }
      }
      if (!flag)
        this.m_defaultDescriptor.AddDeviceJointValuesPair((ITxObject) handledDevice, handledDevice.CurrentPose.JointValues);
    }
    foreach (ITxDevice handledDevice in (Collection<ITxDevice>) handledDevices)
    {
      bool flag = false;
      for (int index = 0; index < this.m_defaultDescriptor.DeviceTCPFLocationPairsCount; ++index)
      {
        if (this.m_defaultDescriptor.GetDeviceTCPFLocationPairAt(index).Key == handledDevice)
        {
          flag = true;
          break;
        }
      }
      if (!flag)
      {
        TxFrame txFrame = (TxFrame) null;
        if (handledDevice is ITxTool)
          txFrame = (handledDevice as ITxTool).TCPF;
        if (handledDevice is ITxRobot)
          txFrame = (handledDevice as ITxRobot).TCPF;
        if (TxFrame.op_Inequality(txFrame, (ITxObject) null))
          this.m_defaultDescriptor.AddDeviceTCPFLocationPair((ITxObject) handledDevice, txFrame.AbsoluteLocation);
      }
    }
  }

  private void AddHandledLocatableObjects(TxObjectList<ITxLocatableObject> handledElements)
  {
    foreach (ITxLocatableObject handledElement in (Collection<ITxLocatableObject>) handledElements)
    {
      bool flag = false;
      for (int index = 0; index < this.m_defaultDescriptor.ElemLocationPairsCount; ++index)
      {
        if (this.m_defaultDescriptor.GetElemLocationPairAt(index).Key == handledElement)
        {
          flag = true;
          break;
        }
      }
      if (!flag)
        this.m_defaultDescriptor.AddElemLocationPair((ITxObject) handledElement, handledElement.AbsoluteLocation);
    }
  }

  private void FillDescriptorWithCurrentValues(KiAppCellPositioningDescriptorEx descriptor)
  {
    for (int index = 0; index < this.m_defaultDescriptor.DeviceJointValuesPairsCount; ++index)
      descriptor.AddDeviceJointValuesPair(this.m_defaultDescriptor.GetDeviceJointValuesPairAt(index).Key, (this.m_defaultDescriptor.GetDeviceJointValuesPairAt(index).Key as ITxDevice).CurrentPose.JointValues);
    for (int index = 0; index < this.m_defaultDescriptor.ElemLocationPairsCount; ++index)
      descriptor.AddElemLocationPair(this.m_defaultDescriptor.GetElemLocationPairAt(index).Key, (this.m_defaultDescriptor.GetElemLocationPairAt(index).Key as ITxLocatableObject).AbsoluteLocation);
    for (int index = 0; index < this.m_defaultDescriptor.DeviceTCPFLocationPairsCount; ++index)
    {
      if (this.m_defaultDescriptor.GetDeviceTCPFLocationPairAt(index).Key is ITxTool)
        descriptor.AddDeviceTCPFLocationPair(this.m_defaultDescriptor.GetDeviceTCPFLocationPairAt(index).Key, (this.m_defaultDescriptor.GetDeviceTCPFLocationPairAt(index).Key as ITxTool).TCPF.AbsoluteLocation);
      else if (this.m_defaultDescriptor.GetDeviceTCPFLocationPairAt(index).Key is ITxRobot)
        descriptor.AddDeviceTCPFLocationPair(this.m_defaultDescriptor.GetDeviceTCPFLocationPairAt(index).Key, (this.m_defaultDescriptor.GetDeviceTCPFLocationPairAt(index).Key as ITxRobot).TCPF.AbsoluteLocation);
    }
  }

  public void UpdateDescriptorWithDefaultValues(
    KiAppCellPositioningDescriptorEx descriptor,
    KiAppCellPositioningDescriptorEx defaultDescriptor)
  {
    for (int index = 0; index < defaultDescriptor.DeviceJointValuesPairsCount; ++index)
    {
      KiAppCellPositioningDescriptorEx positioningDescriptorEx1 = descriptor;
      KeyValuePair<ITxObject, ArrayList> jointValuesPairAt = defaultDescriptor.GetDeviceJointValuesPairAt(index);
      ITxObject key1 = jointValuesPairAt.Key;
      if (!positioningDescriptorEx1.AreDeviceJointValuesStored(key1))
      {
        KiAppCellPositioningDescriptorEx positioningDescriptorEx2 = descriptor;
        jointValuesPairAt = defaultDescriptor.GetDeviceJointValuesPairAt(index);
        ITxObject key2 = jointValuesPairAt.Key;
        jointValuesPairAt = defaultDescriptor.GetDeviceJointValuesPairAt(index);
        ArrayList arrayList = jointValuesPairAt.Value;
        positioningDescriptorEx2.AddDeviceJointValuesPair(key2, arrayList);
      }
    }
    KeyValuePair<ITxObject, TxTransformation> keyValuePair;
    for (int index = 0; index < defaultDescriptor.ElemLocationPairsCount; ++index)
    {
      KiAppCellPositioningDescriptorEx positioningDescriptorEx3 = descriptor;
      keyValuePair = defaultDescriptor.GetElemLocationPairAt(index);
      ITxObject key3 = keyValuePair.Key;
      if (!positioningDescriptorEx3.IsElemLocationStored(key3))
      {
        KiAppCellPositioningDescriptorEx positioningDescriptorEx4 = descriptor;
        keyValuePair = defaultDescriptor.GetElemLocationPairAt(index);
        ITxObject key4 = keyValuePair.Key;
        keyValuePair = defaultDescriptor.GetElemLocationPairAt(index);
        TxTransformation txTransformation = keyValuePair.Value;
        positioningDescriptorEx4.AddElemLocationPair(key4, txTransformation);
      }
    }
    for (int index = 0; index < defaultDescriptor.DeviceTCPFLocationPairsCount; ++index)
    {
      KiAppCellPositioningDescriptorEx positioningDescriptorEx5 = descriptor;
      keyValuePair = defaultDescriptor.GetDeviceTCPFLocationPairAt(index);
      ITxObject key5 = keyValuePair.Key;
      if (!positioningDescriptorEx5.IsDeviceTCPFStored(key5))
      {
        KiAppCellPositioningDescriptorEx positioningDescriptorEx6 = descriptor;
        keyValuePair = defaultDescriptor.GetDeviceTCPFLocationPairAt(index);
        ITxObject key6 = keyValuePair.Key;
        keyValuePair = defaultDescriptor.GetDeviceTCPFLocationPairAt(index);
        TxTransformation txTransformation = keyValuePair.Value;
        positioningDescriptorEx6.AddDeviceTCPFLocationPair(key6, txTransformation);
      }
    }
  }
}
